-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can I relate pivot tables? #26
Comments
Just so I understand what you mean, you're talking about 3 tables right? And you would have something like:
And you want to go from the users table to the pages table? |
yes, it is exactly the case |
Better create mysql view for that, rather than joining in the code. That way it will be easier to maintain. |
You can absolutely do it with a view as well and it will accomplish what you are doing. Then you can actually create a record for that (only for reading). I'm going off of pseudo-code here so it's untested but here is what you would do. Setup 3 new records. $UserRecord = new UserRecord();
$UserRecord->eq('company_id', 12345);
$Records = $UserRecord->findAll();
foreach $Records as $Record) {
foreach($Record->comments as $Comment) { // or whatever you name your relation for comment
echo $Comment->page->url; // or whatever you name your relation for page
}
} Now CAN you do this? Absolutely! You can totally make it work. SHOULD you do this? Honestly probably not. Let's say you have 500 users. So that's 1 query to pull back your users, and then under each foreach() loop you would run a query to pull out their comments (say 2 comments on average). Then for each comment, you would be running a query for each comment to pull the page url out. So in a few lines of code you have just run 1 user query + 500 comment queries + 1000 page queries for each comment. So you've just run 1501 queries against your database where a view would have solved that in one query. |
Is there any example of how I can relate 2 tables through a pivot table?
The text was updated successfully, but these errors were encountered: