Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug when CakePHP Model::save() doesn't attempt an INSERT

Tags:

cakephp

I am having a bear of a time saving the simplest record from a model called ItemView:

if($this->save($this->data)) {
  echo 'worked';
} else {
  echo 'failed';
}

Where $this->data is:

Array
(
    [ItemView] => Array
        (
            [list_id] => 1
            [user_id] => 1
        )
)

And my table is:

CREATE TABLE IF NOT EXISTS `item_views` (
  `id` int(11) NOT NULL auto_increment,
  `list_id` int(11) NOT NULL,
  `user_id` int(11) default NULL,
  `user_ip` int(10) unsigned default NULL,
  `created` datetime NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ROW_FORMAT=FIXED AUTO_INCREMENT=1 ;

Looking at the query dump in debug mode, Cake isn't even attempting an INSERT, so I have no idea how to debug.

Any help would be appreciated.

like image 511
ryonlife Avatar asked Feb 19 '09 18:02

ryonlife


3 Answers

Wow, two miserable hours of my life wasted.

Remember that your beforeSave() must return true!

like image 90
ryonlife Avatar answered Oct 17 '22 16:10

ryonlife


To debug Model->save() check the validation errors and the last sql query

$this->Model->save($data);

debug($this->Model->validationErrors); //show validationErrors

debug($this->Model->getDataSource()->getLog(false, false)); //show last sql query
like image 24
Jan Moritz Avatar answered Oct 17 '22 18:10

Jan Moritz


In cakephp 3.x, you can debug during insert/update

if ($this->TableName->save($entity)) {
      // success 
} else {
// if not save, will show errors 
  debug($entity->errors());

}
like image 9
Saidul Haque Avatar answered Oct 17 '22 18:10

Saidul Haque