Sort linked objects via foreign table field
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
$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>
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
ini_set('display_errors', 1); error_reporting(E_ALL);
Also, get_members(TRUE) returning a legitimate object?
var_dump($model);
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.
// 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)
So, there is something else, _somewhere_ that is causing the issue. I will keep building back up until I can replicate the problem.