I have some more complex needs similar to this which will need to JOIN other entities. I am a little concerned about performance as I add relationships. Is this the correct direction to be going?
$user->nameTheRelationship; //this will output an array of all Persons that are related to User
$user->nameTheRelationship(array(‘condition’ => ‘role = Assistant’)) //this will output an array of Persons that have role Assistant and are related to User.
You can pass the output to a CArrayDataProvider in order to work with the Grid View and etc.
NOTE: You can also achieve this by using Named Scopes and Parameterized Named Scopes which would be faster in most cases. But for now, I think, it is important that you grasp the AR capabilities of Yii.
Great thanks. I wasn’t sure the role field was available with this method. The other issue I am wondering about is doing this without pagination the way you described.
In my example I do this via ActiveDataProvider with Pagination set so the query limits to 40 records. In your example assuming I am an ‘Assistant’ on 50,000 records wouldn’t it try to load 50,000 AR’s exhausting memory?
In the example I gave I am fetching a count of 50,000 but only loading 40 AR’s at a time.
I really appreciate the feedback and will continue to play and learn. This is the kind of performance stuff I am trying to wrap my head around. For example what if I need ‘Person’ with ‘Address’ with ‘PhoneNumber’ where User is in role ‘Assistant’. The amount of Joins starts slowing down a ‘together’ query doesn’t it?
Hmmmmm… maybe I just answered my own question. Lazy loading ‘Address’ and ‘TelephoneNumber’ of the 40 ‘Person’ records on a page is maybe more efficient?
I appreciate the discussion as I learn this. I will experiment and come back.