Get latest SQL query
When debugging CakePHP code, it’s very useful to know which SQL query is executed latest. Use next few lines to get this information $log = $this->Model->getDataSource()->getLog(false, false); debug($log);
When debugging CakePHP code, it’s very useful to know which SQL query is executed latest. Use next few lines to get this information $log = $this->Model->getDataSource()->getLog(false, false); debug($log);
How to return primary key of last inserted record (ID) generated in the last query, like LAST_INSERT_ID() in MySQL or mysql_insert_id() in PHP echo (int)$this->ModelName->getLastInsertID(); No need to explain more, right? 😉
Here is example how to add onChange event in option-select (combo) form element. $packages = array(‘1’=>’First’, ‘2’=>’Second’, ‘3’=>’Third’); // define some select-option element // next will integrate onChange event into it echo $form->input(‘package_id’, array(‘options’ => $packages, ‘label’=>array(‘Package’), ‘onchange’ => “alert(‘Message!’)” )); Of course, $packages array can be defined in controller also (it’s even better to…
Adding new record in database, without raw SQL, but using CakePHP’s methods. Often is needed to add new record in database, when you can not use simple $this->Model->save($this->data) method, and when you do not want to use raw MySQL functions. You can add it on next way: $this->Model->create(); $this->Model->set(‘field_1’, $value_1 ); $this->Model->set(‘field_2’, $value_2 ); $this->Model->set(‘field_3’,…