2012-09-02 32 views
1

比方说,我有两个AR类:DeckCard。每个卡组记录都应该是卡记录的排序列表。当我取甲板纪录,我希望能够把它当作一个CTypedList:Yii:如何使活动记录的行为像一个CTypedList和其他活动记录一样列表元素?

$Deck->save()
$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(); 

后,数据库记录应该反映甲板的新状态,即卡和一个新秩序卡已被删除。

编辑:我的解决方案被称为ListBehavior,一个ActiveRecordBehavior,使AR行为像CLists。该代码是开源的,可作为gist使用。随意使用它并随意编辑。

+0

你应该回答自己的问题,并接受它作为回答这样的问题不留诠释他没有答案列表 – DarkMukke

回答

1
$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();