save() returning false, but with no error in CakePHP

前端 未结 8 1820
一生所求
一生所求 2020-12-15 02:52

My debug value is set to 2, and it\'s displaying all the queries, except the one I need.

I have an Items controller method that is calling

相关标签:
8条回答
  • 2020-12-15 03:26

    CakePHP 3.6

    $entity = $this->Model->newEntity([
        'account_id' => $id,
        'gallery_id' => $gallery_id
    ]);
    
    $result = $this->Model->save($entity);
    
    print_r($entity->getErrors());
    
    0 讨论(0)
  • 2020-12-15 03:29

    contains validationErrors array

    if ($this->save()) {
        return $this->ModelName->id;
    }
    else {
        debug($this->ModelName->invalidFields());
        return false;
    }
    
    0 讨论(0)
  • 2020-12-15 03:30

    This will probably give you the info you need (assuming it's not saving because of invalid data, of course):

    if(!$this->save()){
        debug($this->validationErrors); die();
    }
    
    0 讨论(0)
  • 2020-12-15 03:30

    @cakePHP 3.6 and above: By default, the request data will be validated before it is converted into entities. If any validation rules fail, the returned entity will contain errors. It can be read by getErrors() method. The fields with errors will not be present in the returned entity:

    Say, you have an entity

    use App\Model\Entity\Article;
    
    $entity = $this->ModelName->newEntity([
        'id' => 1,
        'title' => 'New Article',
        'created' => new DateTime('now')
    ]);
    
    $result = $this->ModelName->save($entity);
    
    \Cake\Log\Log::debug($entity->getErrors());
    

    If you’d like to disable validation when converting request data, set the validate option to false:

    $article = $articles->newEntity(
        $this->request->getData(),
        ['validate' => false]
    );
    

    Ref: https://book.cakephp.org/3/en/orm/validation.html

    0 讨论(0)
  • 2020-12-15 03:31

    The other situation where CakePHP fails to report any $this->Model->validationErrors and no other errors is potentially when $this->request->data isn't as Cake expects and is simply ignoring your data, not saving, no validation errors. For example if your data was provided by DataTables you might see this format $this->request->data[0]['Model']['some_field'].

    $this->Model->save($this->request->data[0]) will work however.

    0 讨论(0)
  • 2020-12-15 03:32

    Make sure to check your tables:

    • Does ID have auto increment enabled?
    • Is id your primary key?

    the auto_increment issues killed me. Easy way to check: if any of your rows have ID = 0, auto_increment is likely disabled.

    0 讨论(0)
提交回复
热议问题