Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

How to add non-persistent attributes (fields) to an otherwise normal orm model

I remember having read once that there is a setting with which one can tell the ORM to not throw an error if a model has more fields/attributes than the DB table. However, I have lost the link and cannot find the info in Phalcon's documentation site.

Also, is there a way to declar only some non-persistent fields on a Model? (As opposed to changing the ORM configuration option for the entire application)

thank you

But you can have. Just toArray method will not return them.

edited Dec '16

Thank you for replying, however I don't really understand your response.

I was able to do what I wanted by simply defining the variable on the object but not declaring it as a field of the class.

If I declare the variable as a field of the class and there is no corresponding field in the database, then the ORM complains (throws an error).

Also, if there are more fields in the database table than in the model class, the ORM complains.



114.3k
Accepted
answer
edited Dec '16

I don't have any error which you mention ? Can you post some sample code ? Yes the second is pretty obvious. I have properties defined which are not in table and i don't have any error. Just read docs, check columnMap method etc.

Everything is in docs tbh.

edited Jan '17

Thanks. You were right about not getting exceptions when an object has attributes not found in DB.

And for the sake of anyone searching for this info (had trouble finding it in the docs), you can disable exceptions when a table has columns not defined in columnMap using this setting (phalcon > 2.x)

\Phalcon\Mvc\Model::setup(['ignoreUnknownColumns' => true]);