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

Table prefix - without getSource method

Hi. I'm just starting to look to Phalcons ORM, so this may sound a weird remark, but please bear with me.

I want to use a table prefix, transparent to the developer. I know Phalcon does not have this feature, so i was looking for workarounds. I found the two indicated in this forum - using a superclass or a trait. Neither works very well for me, as it takes the class name too literaly and i'll be working with different developers with different coding standards and ways of naming data base tables. So...

I implemented a class extending Mvc\Model\Manager and rewrote the setModelSource method to include the table prefix. This method is used when the model class invokes setSource - which makes sense. But when the model class rewrites getSource method, then it is 'final' - no way to control from the manager class - and its result is used everywhere.

I thought about trying to change the base class, but I don't think that's right. My question is, wouldn't table prefix be really easy to implement if only the getSource method didn't exist and the model classes had to use setSource method()? Isn't this method just a shortcut? Am i missing something?

Thanks in advance :)

edited May '15

Thanks for the reply Andres. Right, but that's the solution i was talking about - it is based on the class name. And in the project I'm developing it's possible that UsersInfo is related to the table prefix_users_info or prefix_usersinfo, p.e. I didn't want to make a rule so static.

The only solution that comes to my mind is to extend the model setting getSource() as a final method

abstract class MyModel extends \Phalcon\Mvc\Model {
    final public function getSource() {
        return 'prefix_' . parent::getSource();
    }
}

Implemented in the 3.2.x branch.