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

How to automatically format properties before save()?

Hi all,

I have a model with a number of fields for phone numbers. The database is expecting integers, but users can enter any type of character. In my beforeSave() method I have functionality to strip out any non-integer characters, but I've referenced each field individually by name. Is there any way for me to access what the model knows about the database and just automatically iterate through all fields & affect those that are integers?

edited Apr '14

Would behaviours help to achieve your goals. You would likely need to implement your own behaviour, which would allow you to assign your filter and sanitization methods to multiple fields very easily.

With behaviours, I'd still have to set up each field individually. So using behaviours or beforeSave() would really be no different. Regardless of how I set up the formatting, I'd like to be able to do it without explicitely calling each field - if possible.

Is there something unique about the way you have defined those fields in the database? If there is something unique about those fields, then you should be able to hook into that through the models meta data. That way, you don't need to specify each field name, it will happen automatically based on that meta data.

This is the only solution I can see that doesn't involve you specifying the fields you want to act on.

I've defined all those fields as integers. I haven't dealt with meta data much, but that might be the way to go. Thanks for the tip.