Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Select on a related table
  • Hi,
    I have a problem with ORM requests. I have two tables : Patients and Users, related with a many to many relationship.
    I'd like to get all Patients of a User, but I don't want to fetch all the fields of the User's table.
    So, I do : 
    Model_Patient::query()->related('users')->where('users.id',$user_id)->get();

    But it gives me all the fields of the related user, but I don't want it (it contains the username, the hashed password, etc), I want to select specific fields.

    I saw this post and it wasn't posible in January 2012. I want to know if it's possible today. http://fuelphp.com/forums/discussion/7076/how-to-select-only-specific-model-fields-and-not-all-related-fields#Item_5
  • The workaround is to make a model (Patientsusers or so) for the through table too, and define that in both models as a separate relation. You can then do

    Model_Patient::query()->related('patientsusers')->where('patientsusers.users_id', '=', $user_id)->get();

    That will join the through table, but not the users table.

    Alternatively, run the query the other way around:

    if ($user = Model_Users::query()->related('patients')->where('id', '=', $user_id))
    {
        $patients = $user->patients;
    }
  • I don't want to change relationships so I will apply  the second solution, I didn't think about it.
    Thank you.

Howdy, Stranger!

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

In this Discussion