My challenge is to build an Active Record query to get PageClicks where user_id=x. I really stumble on this one as I manage to get data from tables that are directly connected but since I have set no direct relations between the User model and the PageClick model I can’t seem to get data from the PageClick model based on a condition from the User model.
I just figured out that I’m so frustrated I don’t even know if my question makes sense
Summary: One User has one CustomerPage, and one CustomerPage has many PageClick. I want to get all PageClick from the CustomerPage where User is the logged in user (user id is [font="Courier New"]Yii::app()->user->id[/font]).
You don’t tell if you use a CDataProvider but assuming Gii-generated code, you should have a criteria like this in the search() method of the PageClick model (for use with view pageClick/admin):
First of: I just woke up, so I might not have full access to my cognitive functions.
As I understand, your problem lies in accessing a page_click array from a users model (I’m guessing for logging purposes).
Assuming that your relations are set up correctly and $user has an instance of a User row.
$user->customer_page->page_clicks;
Will give you an array of all the click rows the customer page has. You can do this because both BELONGS_TO and HAS_ONE return a single instance so you can directly call relations on them.
Take note though that the above php line will take some resources out of your app. To avoind unnecessary lagging better set with parameters like tri shows or, if you just need a pageview count, use a column in customer_page to increment an INT.
But still I can’t wrap my head around Active Record: How do I present the [font=“Courier New”]$dataProvider->getData()[/font] object in a view file? E.g. if I would like to have all page_clicks in an array. Is there an elegant way of doing this without writing tons of code?
I did not use the Gii CRUD generator and I will narrow my query more later on However, for the time being my query retrieves the data I want (customer_page based on user id and customer_page’s page_clicks based on page id) from the database and returns it like this ([font=“Courier New”]var_dump()[/font] in a view file):
If you just want to display the click count you can add a STAT relation to CustomerPage.
Note, it’s not mandatory to create a dataprovider.
Not tested now, but since you don’t want to write too much code, you should be able to start from User and lazy load the rest. In this case you shouldn’t have to use eager loading in order to specify the condition.
Unfortunately that doesn’t work because customer_page has the [font=“Courier New”]HAS_MANY[/font] relation to page_clicks. If I change to [font=“Courier New”]HAS_ONE[/font] it does work, but page_clicks should return an array.
If customer_page has the relation [font="Courier New"]HAS_MANY[/font] to page_clicks I get the following PHP error when trying to access [font="Courier New"]$user->customer_page->page_clicks[/font]: [font="Courier New"]Trying to get property of non-object[/font]