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

7

40

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 this method in the User model (Item belongsTo User):

function add_basic($email, $password) {
    $this->create();

    $this->set(array(
        'email' => $email,
        'password' => $password
    ));

    if($this->save()) {
        return $this->id;
    }
    else {
        return false;
    }
}

I have confirmed that $email and $password are being passed into the function correctly (and are populated with legit data). email and password are the names of the fields in the User model.

I have also confirmed that on $this->save() it is returning false, but when I view the page where this occurs, the query is not being printed in the debug, and there is no error being thrown, so I have no idea whats going wrong.

Any ideas on how I can see the error, or why the query doesn't seem to be getting executed?

It's weird, cause right after this, I have another model saving data to it in the exact same fashion, it goes off without a hitch.

Brighton answered 22/2, 2010 at 22:7 Comment(1)
To anyone that comes here later. You almost certainly have a validation error. If you cannot id it with the commands shown in the answers here, just go look at the validation in your model classes for what your violation might be.Firebox
C
45

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();
}
Custer answered 22/2, 2010 at 22:45 Comment(6)
+1 Failed validation is almost always the cause when my own saves fail. If you forget to explicitly check your validation, the failure looks like a complete phantom.Ultramicrochemistry
Actually in cakePHP 1.3 you should use debug($this->ModelName->invalidFields());Foot
@23kulpamens -- note the date of my answer ;)Custer
@inkedmn I wrote it for users that came here from Google, in search for fast answer.Foot
this retuns null, what does this mean?Hydrastis
Use cakephp 3 getErrors() mthod. Read more here book.cakephp.org/3.0/en/orm/entities.htmlAdventuresome
H
10

Have you got a beforeValidate() or beforeSave() method in the model or app model? Ifso, are they returning true? Failing that, use a debugger, set a break point in your IDE at the top of cake/libs/models/model.php save() method and step through the code until it returns false. Failing that add die('here'); calls.

Hibernal answered 23/2, 2010 at 20:54 Comment(1)
Holy smokes! This had me stumped, but you saved the day! Thank you; I had no idea beforeSave() had to return true. I was so confused, as CakePHP wasn't returning a single validation error or even trying to insert into the database.Halinahalite
L
8

Try this:

if ($this->save()) {
    return $this->id;
}
else {
    var_dump($this->invalidFields());
    return false;
}
Laurentian answered 22/2, 2010 at 22:13 Comment(0)
A
6

@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

Adventuresome answered 14/11, 2018 at 14:24 Comment(0)
S
3

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.

Sperm answered 2/4, 2014 at 13:45 Comment(1)
after migrating i had that issue... i expend 4 hours of my time thinking. Thanks a lot!Swindle
C
3

CakePHP 3.6

$entity = $this->Model->newEntity([
    'account_id' => $id,
    'gallery_id' => $gallery_id
]);

$result = $this->Model->save($entity);

print_r($entity->getErrors());
Capuchin answered 19/3, 2019 at 11:24 Comment(0)
A
0

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.

Afterwards answered 29/4, 2016 at 12:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.