Zend_Db_Table_Abstract - update? - zend-framework

we have this on Zend Manual:
$table = new Bugs();
$data = array(
'updated_on' => '2007-03-23',
'bug_status' => 'FIXED'
);
$where = $table->getAdapter()->quoteInto('bug_id = ?', 1234);
$table->update($data, $where);
Why do we need getAdapter and quoteInto again? I've read the manual but I don't understand.
What about the save() method, shouldn't we use it instead?
Regards,
MEM

save() is for when you are using Zend_Db_Table_Row if you are using Zend_Db_Table only, update is the method.
The code you've pasted needs to have the getAdapter and quoteInfo because $table is an instance of Bugs but not necessarily Zend_Db_Table_Row or Zend_Db_Table, therefore it isn't connected to the db.

Related

Create model instance in Form in ZF2 like ZF1?

In ZF1 it is possible to create an instance of a model and also access its properties from any form class.`
class Application_Form_Drydepot extends Zend_Form
{
$model = new Application_Model_DbTable_DrydepotModel();
$List = $model ->formationSelect();
array_unshift($List, array('key' => '', 'value' => '--Please Select--'));
$id = new Zend_Form_Element_Hidden('id');
$id->addFilter('Int')
->setDecorators($this->elementDecoration);
$formation = new Zend_Form_Element_Select('formation_id');
$formation->setLabel('Formation Name')
->setRequired(true)
->setAttrib('id', 'formation')
->setAttrib('class', 'required')
->addValidator('NotEmpty', true)
->setMultiOptions($List)
->setDecorators($this->elementDecoration);
}
In here $model directly possible to call but use it easily but zf2 it is quite difficult. I am not successfull about to do it. In ZF2 how do I do it same operation.
Another ways are like here : the documentation
Programmatic Form Creation
ZF2 Coding close to ZF1
use Zend\Captcha;
use Zend\Form\Element;
use Zend\Form\Form;
$captcha = new Element\Captcha('captcha');
$captcha
->setCaptcha(new Captcha\Dumb())
->setLabel('Please verify you are human');
$form = new Form('my-form');
$form->add($captcha);

CakePHP - Using a different model in current model

I am creating a custom validation function in my model in CakePHP. After reading similar questions I have understood that I could be using ClassRegistry::init('Model'); to load a foreign model in my current model. But it doesn't say much more on the syntax and how to actually use it afterwards. This is what I have tried, but nothing "is happening" when I am trying to print the array to see if it contains the right stuff. Basically I want to pull out the User data to use it in my validation.
class Booking extends AppModel {
public $name = 'Booking';
public $validate = array(
'start_time' => array(
'noOptionViolation' => array(
'rule' => 'noOptionViolation',
'allowEmpty' => false
)
),
);
public function noOptionViolation ($start_time) {
$this->User = ClassRegistry::init('User');
$allUsers = $this->User->find('all');
print_r($allUsers);
}
Is this correct syntax? Can I use all the methods of $this->User just like I would in a controller?
You can use import as detailed on this post:
https://stackoverflow.com/a/13140816/1081396
App::import('Model', 'SystemSettings.SystemSetting');
$settings = new SystemSetting();
$mySettings = $settings->getSettings();
In your example it would be like:
App::import('Model', 'Users.User');
$user = new User();
$allUsers = $user->find('all');
print_r($allUsers);
You could better use the import at the beginning of the model.
You could use this too to load Models
$this->loadModel('User');
and access all functions by
$this->User

defaultAdapter in Zend Framework

when i try to construct a query to my db in my model like
class Application_Model_DbTable_Resume extends Zend_Db_Table_Abstract
{
protected $_name = 'users';
public function getFiveLastResume (){
$select= $db->select()->from('users')->order("id DESC")->limit(5);
$stmt = $db->query($select);
$row = $stmt->fetchAll();
return $row;
}
}
so i have an error Notice: Undefined variable: db
if I write adapter before query
$db = Zend_Db::factory('PDO_MYSQL',array(
'host' => '127.0.0.1',
'username' => 'root',
'password' => '',
'dbname' => 'sport'
));
thats work good. why does my adapter not work ?
my application.ini contain right database config,cuz more simply queries work out good without including adapter. im noob in zend, thanks
$db is undefined in the local scope which is why you get the error.
Since you are inside a DbTable object, you can use $this to get the DB adapter:
public function getFiveLastResume () {
$select = $this->select()->from('users')->order("id DESC")->limit(5);
$stmt = $select->query();
$row = $stmt->fetchAll();
return $row;
}
Anywhere else in your application, you should be able to get a reference to the default DB adapter using:
$db = Zend_Db_Table::getDefaultAdapter();
$select = $db->select()->from('table')...;
This of course assumes you have created a Zend_Db_Table object and set it as the default adapter.

Zend_db caching

Is there a way to caching resultsets in Zend_db? For example, I want to run a select query using Zend_db and want this query to be cached to be able to run it faster later.
My advice is that create a initialization method in Bootstrap.php with prefix "_init". for exaple :
/**
*
* #return Zend_Cache_Manager
*/
public function _initCache()
{
$cacheManager = new Zend_Cache_Manager();
$frontendOptions = array(
'lifetime' => 7200, // cache lifetime of 2 hours
'automatic_serialization' => true
);
$backendOptions = array(
'cache_dir' => APPLICATION_PATH . '/cache/zend_cache'
);
$coreCache = Zend_Cache::factory(
'Core',
'File',
$frontendOptions,
$backendOptions
);
$cacheManager->setCache('coreCache', $coreCache);
$pageCache = Zend_Cache::factory(
'Page',
'File',
$frontendOptions,
$backendOptions
);
$cacheManager->setCache('pageCache', $pageCache);
Zend_Registry::set('cacheMan', $cacheManager);
return $cacheManager;
}
By this way, you have created and injected your cache manager with the caches which you need in your app.
Now you can use this cache object where you want to use.
For instance, in your controller or where else :
/**
*
* #return boolean |SimplePie
*/
public function getDayPosts()
{
$cacheManager = Zend_Registry::get('cacheMan');
$cache = $cacheManager->getCache('coreCache');
$cacheID = 'getDayPosts';
if (false === ($blog = $cache->load($cacheID))) {
$blog = Blog::find(array('order' => 'rand()', 'limit' => 1));
$cache->save($blog, $cacheID);
}
// do what you want to do with the daya you fetched.
}
You can use Zend_Cache when you want to save result-sets.
Zend_Db doesn't do any result-set caching itself. It's left for you to do it an application-specific way, because the framework has no way of knowing which result-sets need to be cached for performance reasons, versus those that can't be cached because you need them to be absolutely current. Those are criteria only you as the application developer know.
Just googling for "zend_db cache results" the first match is this blog showing how to use a Zend_Cache object to save a db query result: Zend Framework:: Caching the database query results

Need help with Zend_db update

Currently I'm working with Zend framework and I need help with Zend_db update in Zend_Db_Table_Abstract class.
Here is my SQL statement
UPDATE user
SET password = '$password',
`enter code here` WHERE email = '$email'
Here is my code in zend_db
public function updatePassword($password,$email)
{
$data = array(
'password' => $password
);
$where = "email = '". $email ."'";
$this->update($data, 'email = '.$email);
}
This only work if I update using int id as my where clause but I wanted to use a email string as a where clause.
Can someone please help me the best way to achieve this?
I wanted to be secure and avoid SQL Injection attack
Thanks so much in advance.
You approach only works with integer values, because the way you concat the where string does not escape the value. So if you do
'email = '.$email
It will product an sql string like this if you use the string "hello world"
WHERE email = hello world
This is an invalid SQL statement so the update does not happen. What you want to produce is a where clause like this
WHERE email = 'hello world'
There are multiple ways to do this, but the safest way to do that via Zend Framework is described in the reference manual under "Example #24 Updating Rows Using an Array of Arrays".
$data = array(
'password' => $password
);
$where['email = ?'] = $email;
$this->update($data, $where);
This code might help you :
public function updateDetails($data, $emailId)
{
$where = array('email = ?' => $emailId);
$this->update($data, $where);
}
Please let me know if you still face the problem.....?