假设我有两个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()
之后,数据库记录应反映牌组的新状态,即牌的新顺序和一张牌已被移除。
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();