22

我有时间从一个名为 ItemView 的模型中保存最简单的记录:

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

$this->data 在哪里:

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

我的桌子是:

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 ;

在调试模式下查看查询转储,Cake 甚至没有尝试插入,所以我不知道如何调试。

任何帮助,将不胜感激。

4

8 回答 8

25

哇,浪费了我生命中两个悲惨的小时。

请记住,您beforeSave()必须返回true

于 2009-02-19T18:32:02.220 回答
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
于 2013-12-10T18:48:48.083 回答
9

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

}
于 2016-03-07T12:13:15.657 回答
8

总是让我明白的是,如果我修改了实际的表(通常是通过向它添加属性),那么你必须刷新你的缓存。通常只需删除以下两个文件夹中的所有内容对我来说就可以了:

tmp > cache > models 
tmp > cache > persistent
于 2012-04-17T20:31:16.597 回答
1

您使用的控制器名称是什么?

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);
            }
        }
    }
    ?>
于 2013-05-30T18:35:47.940 回答
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

于 2013-06-24T16:54:27.387 回答
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);

}
于 2013-09-27T07:58:41.817 回答
1

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

Solution: I forget to this:

array('post', 'put')
于 2015-08-15T18:40:16.773 回答