Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to Select only specific model fields and not ALL related fields?
  • 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.
    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
  • You already can, see You can do things like
    ->where('comments.user_id', 1)
    to add a where clause on a related object.
  • 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 = 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 = 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,
  • Currently, the ORM will select all columns. There are no plans to change that at the moment.
  • 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?
  • Yes, it is still the case, the ORM is not a query builder.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion