我试图打印包含记录的总列表,对于此列表,有些可能与用户连接。活动项目来自同一班级的另一个集合(活动集合)。我会对整个列表进行foreach,并且需要检查每个记录是否存在于活动集合中。 有没有这个功能?Kohana 3.2 ORM检查集合中是否存在记录
现在我把活动项放在一个数组中,记录ID作为数组键以检查,并且工作正常,但我想知道是否有更好的方法来做到这一点?
$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>";
}
}
任何想法?
感谢您的反馈!我确实希望已经有这样的功能,你的回答会增加很多查询,所以我想我会坚持当前的逻辑。我使用smarty,所以布局和逻辑是分开的。但对于这个例子来说,这更简单。不管怎么说,还是要谢谢你! – mrBrown 2013-02-11 14:04:50