Yii:如何使一个活动记录表现得像CTypedList,并将其他活动记录作为列表元素


Yii: How to make an active record behave like a CTypedList with other active records as list elements?

假设我有两个AR类:DeckCard。每个卡片组记录应该是卡片记录的排序列表。当我获取一个甲板记录时,我希望能够像对待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()之后,数据库记录应反映牌组的新状态,即牌的新顺序和一张牌已被移除。

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

$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(); 
相关文章: