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

What's the principle when phalcon gets the table name of a model ?

I use the small case of model name as table name, that is, if the model name is OrderInfo, then the table name is orderinfo. So I didn't override the method getSource() and all models had been working well.

But in phpunit tests, all models started to fetch meta data of tables whose names are in a form like 'order_info'. When I overrided the model's getSource() method, it worked well again.

I use phalcon 1.2.4, what's the principle when phalcon gets the table name of a model, and what can I do ?

Thanks.



12.6k
edited Nov '14

I'm not certain that I understand what you mean by principle, but Phalcon looks for the tables to have underscores - as you are seeing.

I would suggest you instead name your tables with underscores is this is what it looks for. Otherwise just use the getSource method and adjust it to be how you want it.

If for some reason you must have your tables named in all lowercase as opposed to how Phalcon expects them to be, you could simply make a new base model and then override the getSource method there so that it automatically applies to all of your models.

If phalcon looks for tables to have underscores, why there is no problem when the code is running normally ?

I'm not certain that I understand what you mean by principle, but Phalcon looks for the tables to have underscores - as you are seeing.

I would suggest you instead name your tables with underscores is this is what it looks for. Otherwise just use the getSource method and adjust it to be how you want it.

If for some reason you must have your tables named in all lowercase as opposed to how Phalcon expects them to be, you could simply make a new base model and then override the getSource method there so that it automatically applies to all of your models.

By default Phalcon model will reference to a table with a similar name. I think it will be best practise to prefix your tables in the database for any particular application. On this case Prefix_tablename.

Thank you for your answer. Maybe I didn't explain my question clearly, but this has nothing to do with table prefix.

That is, when I use models in phpunit tests, they will try table names with underscores (model OrderInfo try to find table order_info), but when I use them normally, they will try table names without underscores (model OrderInfo try to find table orderinfo).

By default Phalcon model will reference to a table with a similar name. I think it will be best practise to prefix your tables in the database for any particular application. On this case Prefix_tablename.