Hi,
I have 2 models for example: Model_Post -> has_many -> Model_Commment
Where for example Model_Comment has a 'user_id' field
What I would like to do is to select all Model_Post's where a given user has written a comment on the post but NOT to get the specific details about the comments.
I can do this using something like: Model_Post::query()->related('comments', array('where' => array(array('user_id', '=', $user_id)))->get();
However, this currently gets me ALL the Posts along with the nested comments. I don't however want to have the comment data actually retrieved from the DB and/or turned into the nested Comment models on each post object.
I tried adding a "->select(POST, FIELDS, HERE)" but this only limits the fields being selected from the main model (ie. Model_Post) and the fields for the related models are still ALL included in the result.
Does any one know of a specific function I can use to limit the selection? Right now I'm having to do the query, load the data and then loop over the result and unset the related Comment model objects but this feels messy. I also want to try and avoid extending/overwriting the main ORM package to add this functionality too!
Hope this makes some sense.
Richard
PS: It would be nice if you could access all the functionality of the Core Database Query functions within the ORM ie. Grouping etc too!
I think this would be a useful feature, particularly when selecting data for AJAX requests where I don't want to return all fields. It would be nice to just JSON encode the models straight from ORM. Currently I have to pull the data out of the models first which can get quite tedious if there are lots of nested models... unless someone can suggest a better approach
Thanks for replying but this doesn't solve the problem I'm having as it still fetches ALL of the related models objects and hydrates them. I just want to use the related table to do some filtering but not actually get data from the extra table(s)
Basically I want SQL like:
SELECT posts.* FROM posts JOIN comments ON comments.post_id = posts.id WHERE comments.user_id = 1
and because it would not have any of the columns from the comments table it wouldn't hydrate the comments property of each post object
But its running
SELECT posts.*, comments.* FROM posts JOIN comments ON comments.post_id = posts.id WHERE comments.user_id = 1
and them hydrating each of the comments properties for each post object
Regards,
Richard
PS: For anyone else who might try WanWizards fix you also need to add in a ->related('comments') before the ->where('comments.user_id')
Are there some news on this?
I want to have related data from a users table, but do not want to fetch all data (because of password hash and so on).
This does not work, according to docs it is only where and order_by statements?
Right now I will write a workaround to wipe out unnecessary fields from the array, but it would be nice if that you could be solved using db-class/ORM package in future. Or is it even possible by now?
Cheers,
Matthias
I would like to select only certain columns of a related model. The post above tells me this is not possible, but was posted seven years ago. Is this still the case?