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.

Models relationship

Is it possible to create a relationship between models that have different connection? Thanks in advance!



44.7k
edited Dec '15

I don't think that its possible to join between two tables on different databases. If you really need to do that then you might want to consider breaking up the schemas into a different way.

In my project I have an org database and a site database. The Sites model is stored in the org database and to access the service for the site database a Sites model must be first selected with $site->select() or otherwise an exception will be thrown in the site service definition.

So then you have two different databases and basically you are going to need to do joins in PHP land. Maybe its not a big deal if the join is over a small array of values and the search was on an index. The Big-O runtime isn't going to be more and you will still have the same time on the searches and with just a small overhead of passing values around in your program. If its just dozens or maybe a hundred values and you know that it won't expand out to be thousands then its very neglible. If its just an administrative function then its not important at all.



885
Accepted
answer

Hi! Thanks for your comment and suggestion. I finally manage to do it. All you need to do is make sure that you initialize your model with the correct database connection and proper namespace of the Model Class you're creating relationship with.



44.7k

You are creating a relationship but is capable of joining with PHQL?

No sir, I haven't tried it.

You are creating a relationship but is capable of joining with PHQL?