1

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

编辑:我的解决方案称为 ListBehavior,这是一种使 AR 表现得像 CLists 的 ActiveRecordBehavior。该代码是开源的,可作为gist获得。随意使用它并根据需要进行编辑。

4

1 回答 1

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(); 
于 2013-05-15T07:18:11.670 回答