我有一个拥有多个头像和代理商的游戏模型。当我删除游戏时,我想清理所有剩余的数据,所以我还要删除所有具有相应game_id
的头像和代理:
namespace app\models;
use app\models\Avatars;
use app\models\Agents;
class Games extends \lithium\data\Model
{
public static function __init($options = array()) {
parent::__init($options);
$self = static::_instance(__CLASS__);
Games::applyFilter('remove', function($self, $params, $chain) {
$conditions = array( 'game_id' => $params['conditions']['_id'] );
$message = new \app\extensions\helper\Message();
$debugString = var_export($conditions, true);
$message->addDebugMessage("params:{$debugString}");
//Output:
//params:array ( 'game_id' => '4f301f69a170c8cf52000002', )
if(!Agents::remove($conditions)) { $message->addErrorMessage('Es konnten nicht alle Agents geloescht werden.'); };
if(!Avatars::remove($conditions)) { $message->addErrorMessage('Es konnten nicht alle Avatare geloescht werden.'); };
return $chain->next($self, $params, $chain);
});
}
}
虽然游戏被删除,但代理和头像仍留在MongoDB中。 有人对此有暗示吗?
db
中的代理示例>db.agents.find()
{ "_id" : ObjectId("4f301f71a170c8391f000000"), "game_id" : ObjectId("4f301f69a170c8cf52000002"), "type" : "army", "subtype" : "deer", "units" : 5, "xPos" : 5, "yPos" : 5 }
答案 0 :(得分:3)
我必须要看,但我不认为remove()
会施放值。你需要这样做:
$conditions = array( 'game_id' => new MongoId($params['conditions']['_id']));