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

后端 未结 8 1954
一个人的身影
一个人的身影 2021-01-03 23: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         


        
相关标签:
8条回答
  • 2021-01-03 23:29

    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
    
    0 讨论(0)
  • 2021-01-03 23:30

    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);
                }
            }
        }
        ?>
    
    0 讨论(0)
  • 2021-01-03 23:30

    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

    0 讨论(0)
  • 2021-01-03 23:35

    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
    
    0 讨论(0)
  • 2021-01-03 23:40

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

    Solution: I forget to this:

    array('post', 'put')
    
    0 讨论(0)
  • 2021-01-03 23:44

    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());
    
    }
    
    0 讨论(0)
提交回复
热议问题