Php Yii:如何使一个活动记录像一个CTypedList一样,将其他活动记录作为列表元素?

Php Yii:如何使一个活动记录像一个CTypedList一样,将其他活动记录作为列表元素?,php,activerecord,data-structures,yii,sortedlist,Php,Activerecord,Data Structures,Yii,Sortedlist,假设我有两个AR类:Deck和Card。每个卡片记录都应该是卡片记录的排序列表。获取数据组记录时,我希望能够将其视为CTypedList: $Deck = Deck::model()->findByPk(1); // Retrieve the deck $Card = $Deck->removeAt(0); // draw the first card (and remove it from the deck) $Deck->shuffle(); // shuffle the

假设我有两个AR类:
Deck
Card
。每个卡片记录都应该是卡片记录的排序列表。获取数据组记录时,我希望能够将其视为CTypedList:

$Deck = Deck::model()->findByPk(1); // Retrieve the deck
$Card = $Deck->removeAt(0); // draw the first card (and remove it from the deck)
$Deck->shuffle(); // shuffle the deck
$n = $Deck->count(); // get the number of remaining cards in the deck
$Deck->save(); 
$Deck->save()
之后,数据库记录应反映卡片组的新状态,即新的卡片顺序和一张卡片已被移除


编辑:我的解决方案称为ListBehavior,这是一种ActiveRecordBehavior,使AR的行为类似于Clist。该代码是开源的,可以作为。您可以随意使用和编辑它。

您应该回答自己的问题并将其作为答案接受,这样问题就不会保留在未回答列表中。您应该回答自己的问题并将其作为答案接受,这样问题就不会保留在未回答列表中
$Deck = Deck::model()->findByPk(1); // Retrieve the deck

$Card = $Deck->removeAt(0); // draw the first card (and remove it from the deck)

shuffle($Deck); // shuffle the deck

$n = $Deck->count(); // get the number of remaining cards in the deck

$Deck->save();