Ejemplo n.º 1
0
 public function execute(DB $aDB, Model $aModel)
 {
     $aPrototype = $aModel->prototype();
     $aDelete = new Delete($aPrototype->tableName());
     // 设置limit
     $aDelete->criteria()->setLimit(1);
     // delete 当前model
     foreach ($aPrototype->keys() as $sClmName) {
         if ($aModel->changed($sClmName)) {
             //主键发生修改
             throw new ORMException('org\\jecat\\framework\\mvc\\model\\db\\orm\\Updater : Key 有修改,无法进行Delete操作');
         } else {
             //用主键作为查询条件
             $aDelete->criteria()->where()->expression(array(SQL::createRawColumn(null, $sClmName), '=', SQL::transValue($aModel->data($sClmName))), true, true);
         }
     }
     $aDB->execute($aDelete);
     // 仅delete hasAndBelongsTo的桥表
     foreach ($aPrototype->associations() as $aAssociation) {
         $aAssociatedModel = $aModel->child($aAssociation->name());
         if (!$aAssociatedModel) {
             continue;
         }
         switch ($aAssociation->type()) {
             case Association::hasOne:
                 $this->setAssociatedModelData($aModel, $aAssociatedModel, $aAssociation->fromKeys(), $aAssociation->toKeys());
                 if (!$aAssociatedModel->delete()) {
                     return false;
                 }
                 break;
             case Association::belongsTo:
                 // nothing todo ...
                 break;
             case Association::hasMany:
                 $this->setAssociatedModelData($aModel, $aAssociatedModel, $aAssociation->fromKeys(), $aAssociation->toKeys());
                 if (!$aAssociatedModel->delete()) {
                     return false;
                 }
                 break;
             case Association::hasAndBelongsToMany:
                 // delete bridge table
                 foreach ($aAssociatedModel->childIterator() as $aOneChildModel) {
                     $this->deleteBridge($aDB, $aAssociation, $aModel, $aOneChildModel);
                 }
                 break;
         }
     }
     return true;
 }