22

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.

ryonlife
  • 6,563
  • 14
  • 51
  • 64

8 Answers8

25

Wow, two miserable hours of my life wasted.

Remember that your beforeSave() must return true!

mauris
  • 42,982
  • 15
  • 99
  • 131
ryonlife
  • 6,563
  • 14
  • 51
  • 64
24

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
Jan Moritz
  • 2,145
  • 4
  • 23
  • 33
10

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());

}
Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
Saidul Haque
  • 606
  • 7
  • 11
8

What always gets me is that if I modify the actual table (usually by adding attributes to it), then you must flush your cache. Usually just deleting everything inside the following two folders does the trick for me:

tmp > cache > models 
tmp > cache > persistent
rodrigo-silveira
  • 12,607
  • 11
  • 69
  • 123
1

What is the name of controller you are using?

Mismatching the name of controller and model also cause some errors. If you are using Posts controller then the Post model should be used. If you are using Post model inside other controller; say BlogsController then the Post model should be loaded like following:

    <?php
    class BlogsController extends AppController
    {
        public function index()
        {
            $this->loadModel("Post");    
            //Here I'm loading other model inside blogs controller
            if($this->request->is('post'))
            {
                $this->Post->save($this->request->data);
            }
        }
    }
    ?>
Aboutblank
  • 697
  • 3
  • 14
  • 31
1

It may be, Validations returns false.. You can check like

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

Put false and check whether now data inserts, if it is then it is validation error

samayo
  • 16,163
  • 12
  • 91
  • 106
1

may be something like this

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

$errors = $this->Model->validate();// or perhaps validationError(), please confirm

if(!$errors) {
   $this->Model->save();
}else{
   //show errors
   pr($errors);

}
Sumit Kumar
  • 1,855
  • 19
  • 19
1

Same problem here. I wasted 2 hours of my life.

Solution: I forget to this:

array('post', 'put')
user1999
  • 87
  • 1
  • 10