We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

model->save() return TRUE when no matching database column

I'm testing my app, it is in a good state and then I change the column name (not at model, but really at the database), tested on pure MySQL query and it is an error. However the model->save() return TRUE, why? I'm using save build, could this be the reason? do you know under what particular situation will model::save return FALSE?


update: found a possibly related stackoverflow


FYI it is UPDATE not CREATE



2.1k

could you show some codes



25.7k

thanks for your reply @7thcubic, they're the simplest code, basically no different than the codes you see in the doc examples. And the point is model::save is supposed to return FALSE if fail, but it's returning TRUE and without generating any server side error.



2.1k

is it because your database allow's null? basically if u changed the name in database but not the model, the field is null. so if you field that is changed accept null. its perfectly alright.



25.7k

@7thcubic, I'm sorry, what do you mean "database allows null"? do you mean the column allows null? I believe it is not the issue; like I said I tested with plain MySQL query and it clearly gets an "unknown column" error.



2.1k
class Model
{
    public $field1;
    public $field2NotValid;
    public $field3;

    public function metaData()
    {
        ....
        //field1, field3, field4
    }
}

https://docs.phalcon.io/en/latest/reference/models.html#models-meta-data

the sql generated by the model save will consist of field1, field3, null for field4

and if field4 accepts null, meaning it is NOT NULL unchecked in sql database. IT WILL NOT GENERATE ANY ERROR =)



25.7k
edited Jan '15

The problem is not the input value @7thcubic, it is "unknown column", the column "not even exist" because the column name doesn't match. Try execute "UPDATE table_name SET incorrect_column_name = some_value"

edited Jan '15

The problem is not the input value @7thcubic, it is "unknown column", the column "not even exist" because the column name doesn't match. Try execute "UPDATE table_name SET incorrect_column_name = some_value"

Yes, if your model does not have defined column map however, it will ignore all extra fields you throw at it that don't map.

As explained above:

If the new column name is not included in your saved items, and the column allows NULL it will simply put NULL

If the new column name is not included in your saved items, and the column is NOT NULL, it will throw error and return false.



25.7k

thanks for your reply @trantremseyer. The column is not allowing NULL and it returns true.

thanks for your reply @trantremseyer. The column is not allowing NULL and it returns true.

Ok, but are you Creating or Updating. If you are updaing, the column would already be set to something



25.7k

Yes it is UPDATE. Logically, if a query fail it's supposed to have error isn't it?

thanks for your reply @trantremseyer. The column is not allowing NULL and it returns true.

Ok, but are you Creating or Updating. If you are updaing, the column would already be set to something

That is what I am trying to figure out, if it is actually failing. Because if you are sending an update command, then the field that is not null, was already set in the database, so it will not throw an error on update if that field is not sent. You can try to add a validator directly to your model before save etc to check for that field directly. Also make sure your /cache/metaData/ does not have a different layout in it (delete if old). Again, If you are relying only on the mysql error, it will not create on on an update if the field is set already.

Yes it is UPDATE. Logically, if a query fail it's supposed to have error isn't it?

thanks for your reply @trantremseyer. The column is not allowing NULL and it returns true.

Ok, but are you Creating or Updating. If you are updaing, the column would already be set to something



25.7k

Yes it is actually failing as I repeated above; it doesn't do anything to the database and it is a total fail.

That is what I am trying to figure out, if it is actually failing. Because if you are sending an update command, then the field that is not null, was already set in the database, so it will not throw an error on update if that field is not sent. You can try to add a validator directly to your model before save etc to check for that field directly. Also make sure your /cache/metaData/ does not have a different layout in it (delete if old). Again, If you are relying only on the mysql error, it will not create on on an update if the field is set already.

Yes it is UPDATE. Logically, if a query fail it's supposed to have error isn't it?

thanks for your reply @trantremseyer. The column is not allowing NULL and it returns true.

Ok, but are you Creating or Updating. If you are updaing, the column would already be set to something

Hmm, ok I had a simliar issue recently and did this

"Also make sure your /cache/metaData/yourmodel.php does not have a different layout in it (delete if old). "

It will rewrite itself. I deleted 5 columns and the model still thought they existed.

Yes it is actually failing as I repeated above; it doesn't do anything to the database and it is a total fail.

That is what I am trying to figure out, if it is actually failing. Because if you are sending an update command, then the field that is not null, was already set in the database, so it will not throw an error on update if that field is not sent. You can try to add a validator directly to your model before save etc to check for that field directly. Also make sure your /cache/metaData/ does not have a different layout in it (delete if old). Again, If you are relying only on the mysql error, it will not create on on an update if the field is set already.

Yes it is UPDATE. Logically, if a query fail it's supposed to have error isn't it?

thanks for your reply @trantremseyer. The column is not allowing NULL and it returns true.

Ok, but are you Creating or Updating. If you are updaing, the column would already be set to something



25.7k
edited Jan '15

no, I guess you mean the "Phalcon\Mvc\Model\MetaData\Files"? I'm using "Phalcon\Mvc\Model\MetaData\Memory" so maybe that's not the cause of the problem.

I truly appreciate your help anyway.