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 set a different table name for a specific model?

Hi There,

Case 1: I have been looking for a way on how to set the model with a different table name. I found setSource() under Model but I don't see any example to use it. I tried ways to use it like calling it from a constructor but it does not allow me to use a constructor on a model that inherits the Model class.

How am I suppose to use this or what is the appropriate to do this? I am using micro framework by the way in case the implementation seems to be different and all.

Case 2: How do we deal with multi databases too? I noticed in the example that we need to set different connections for each but I find this inefficient in my case. I have several databases but these databases interact and I have queries that gets data from tables residing from different databases. How do we implement this one in Phalcon? Much preffered though using only the current connection instead of creating different instances and just play around it via query.

Thanks a lot, icewing02



2.1k
Accepted
answer

Case 1:

class YourModelname extends \Phalcon\Mvc\Model
{
    public function getSource()
    {
        return "your_database_name";
    }
}

Case 2: Take a look on Relationships between Models



3.3k

Thanks Arthur for the prompt reply! Sorry it took me a while to get back. I will be testing it today and see if things works perfectly here, and go back here for updates.



3.3k

Sorry for the late reply. Cheers! Things are working well. Two thumbs up!