Sort linked objects via foreign table field

edited March 2013 in Modules
In the case of an data model that uses a 'has_many' relationship, is there a way to sort the returned related objects based on a field from that table?

For example: I have a 'committees' which 'has_many' 'teammembers'.

In my view I have:
$committees = fuel_model('committees'); <? foreach($committees as $comm): ?> <? foreach ($comm->members as $member): // uses has_many relationship ?> <li><?= $member->member_name ?></li>

but the return array from `comm->members` is not sorted, or it is not sorted according to the _common_query order_by statement in the 'teammembers' model.

What i want to do is be able to sort the results of $comm->members based on $member->last_name.

I could of course make a new array, sort that, then do output ... but it seems like it should be something possible with the underlying queries.

Comments

  • edited 5:05AM
    If you do $comm->get_members(TRUE), it will return the teammembers_model with the active record where_in statement already applied so you can perform additional active record calls like so:
    $committees = fuel_model('committees'); <? foreach($committees as $comm): $model = $comm->get_members(TRUE); $model->db()->order_by('last_name', 'asc'); $members = $model->find_all(); ?> <? foreach ($members as $member): // uses has_many relationship ?> <li><?= $member->member_name ?></li>
  • edited March 2013
    Thank you ... the argument to return the teammembers_model helped me to get the result set ordered the way I wanted ... But a new problem has arisen.

    Now, I have an obvious error in the FUEL/CI processing, as I get a display of all the content from my 'view' (including sorted teammember names) but there is no template. just an empty html fragment, without any of the header blocks, css, js, etc included. This is usually indicative of a PHP or other processing error ... but I can find nothing in the CI logs, nor apache logs to suggest where the error might be.

    This is triggered when ever I call any method of the model instance:

    $model = $comm->get_members(TRUE); $model->db()->order_by('last_name', 'asc'); <---- triggers

    If I use any model method such as `echo $model->table_name()` the table_name will be output inside the view.

    I've been frustrated by this phenom in the past, because of the lack of logging of errors. ( i have all error logging enabled)

    I have merged in changes from branch 1.0 up to SHA 7a96327674f1
  • edited 5:05AM
    Are you in the DEVELOPMENT environment according the index.php bootstrap? This will turn on error handling:
    ini_set('display_errors', 1); error_reporting(E_ALL);
    Also, get_members(TRUE) returning a legitimate object?
    var_dump($model);
  • edited 5:05AM
    Yes, my logging is on.
    Yes, the object is valid. I can call methods on it, and the methods return the expected results.

    Doing a `var_dump` I have `object(Teammembers_model)#95 (50) { ...`

    Usually when I see a FUEL page behave like this (output the view, but without any template, etc) it's either a PHP syntax error, or a DB error. And it usually involves some sort of error suppression, which is why there is no logging of it to application logs or apache logs.
  • edited 5:05AM
    So if you comment out the order_by, does it still error?
  • edited 5:05AM
    Yes. And it turns out I don't need the 'order_by' anyway, as that is in the _common_query in the model.

    // get the raw members model for this committe so we can sort members // see has_many /admin/tools/user_guide/general/models#relationships $comm_member_model = $comm->get_members(true); // $comm_member_model->db()->order_by('last_name', 'asc'); // $members = $comm_member_model->find_all(); // $comm_member_model->debug_data();

    Any of the lines above that call a method on the $comm_member_model object results in the error. (But keep in mind ... the correct, sorted data is being returned and populates the view)
  • edited 5:05AM
    I guess I'm confused when you say that the correct sorted data is being returned and populates the view. I thought that it was turning up an empty HTML fragment. If you uncomment those lines and add an exit right after the debug_data(), do you see any errors?
  • edited 5:05AM
    ... and in trying to simplify my code down to the bare basics in order to share demonstration code ... I can't reproduce the issue. (Should have tried that first I suppose)

    So, there is something else, _somewhere_ that is causing the issue. I will keep building back up until I can replicate the problem.
Sign In or Register to comment.