I’m trying to print a total list with records, for this list, some might be connected to a user. The active items are from another collection (the active collection) of the same class. I’ll foreach over the total list and need to check for every record if it extists in the active collection.
Is there a function for that?
At the moment I put the active items in an array with the record id as array key to check on, and that works, but I wondered if there is a nicer way to do this?
$totalcollection = ORM::Factory('table')->find_all();
// user has an relation named 'activerecords', which will find the records connected
// to a user through a link table
$activecollection = $user->activerecords->find_all();
foreach( $totalcollection as $record ) {
$active = false;
// I'm looking for a function like this one. Does it exist?
$active = $activecollection->contains($record);
if($active) {
echo "<li class=\"active\">".$record->name."</li>";
} else {
echo "<li>".$record->name."</li>";
}
}
Any ideas?
You could write a method for that in your
Model_TableAnd then in your loop
Another option, if you think this will generate too many SQL queries, would be to get the activerecord ids and check with
in_array(), I think thats the way you’re doing it at the moment. Would be fine, too, IMO.BTW: Please consider separating your layout from your logic, e.g. with KOstache.