Example #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;
 }
Example #2
0
 public function child($name, $aShareChild = false)
 {
     if (is_int($name) and $name >= 0) {
         if (!isset($this->arrDataSheet[$name])) {
             return null;
         } else {
             // 返回共享的 child 对像
             if ($aShareChild) {
                 $this->seek($name);
                 return $this->shareModel();
             } else {
                 if (!$this->arrAloneChildren or !isset($this->arrAloneChildren[$name])) {
                     if (!($aPrototype = $this->prototype())) {
                         return null;
                     }
                     $this->arrAloneChildren[$name] = $aPrototype->createModel(false);
                     // 数据共享, 指针独立
                     $this->arrAloneChildren[$name]->arrDataSheet =& $this->arrDataSheet;
                     $this->arrAloneChildren[$name]->nDataRow = $name;
                 }
                 return $this->arrAloneChildren[$name];
             }
         }
     } else {
         return parent::child($name);
     }
 }
Example #3
0
 public function execute(DB $aDB, Model $aModel)
 {
     $aPrototype = $aModel->prototype();
     $aInsert = new Insert($aPrototype->tableName());
     // 从 belongs to model 中设置外键值
     foreach ($aPrototype->associations() as $aAssociation) {
         if ($aAssociation->isType(Association::belongsTo)) {
             if (!($aAssocModel = $aModel->child($aAssociation->name()))) {
                 continue;
             }
             if (!$aAssocModel->save()) {
                 return false;
             }
             $this->setAssocModelData($aAssocModel, $aModel, $aAssociation->toKeys(), $aAssociation->fromKeys());
         }
     }
     // -----------------------------------
     // insert 当前model
     $bTableLocked = false;
     $sTableName = $aPrototype->tableName();
     $e = null;
     try {
         // 检查/自动生成 主键值
         if ($arrKey = $aPrototype->keys()) {
             foreach ($arrKey as $sKeyName) {
                 if ($aModel->data($sKeyName) === null) {
                     $aClmRef = $aPrototype->columnReflecter($sKeyName);
                     if (!$aClmRef->isAutoIncrement()) {
                         // 锁表
                         if (!$bTableLocked) {
                             $aDB->execute("LOCK TABLES `{$sTableName}` WRITE ;");
                             $bTableLocked = true;
                         }
                         // 生成并设置主键值
                         $keyValue = AutoPrimaryGenerator::singleton()->generate($aClmRef, $sTableName, $aDB);
                         $aModel->setData($sKeyName, $keyValue);
                     }
                 }
             }
         }
         // 设置数据
         foreach ($aModel->dataNameIterator() as $sClmName) {
             $value = $aModel->data($sClmName);
             if ($value !== null) {
                 $aInsert->setData($sClmName, $value);
             }
         }
         // 执行 insert
         $aDB->execute($aInsert);
     } catch (\Exception $e) {
     }
     // 解锁后再抛出异常
     if ($bTableLocked) {
         $aDB->execute("UNLOCK TABLES ;");
     }
     if ($e) {
         throw $e;
     }
     // 自增形主键
     if ($sDevicePrimaryKey = $aPrototype->devicePrimaryKey() and $aModel->data($sDevicePrimaryKey) === null) {
         $aModel->setData($sDevicePrimaryKey, $aDB->lastInsertId(), false);
     }
     // -----------------------------------
     // insert 关联model
     foreach ($aPrototype->associations() as $aAssociation) {
         $aAssocModel = $aModel->child($aAssociation->name());
         if (!$aAssocModel) {
             continue;
         }
         switch ($aAssociation->type()) {
             case Association::hasOne:
                 $this->setAssocModelData($aModel, $aAssocModel, $aAssociation->fromKeys(), $aAssociation->toKeys());
                 if (!$aAssocModel->save()) {
                     return false;
                 }
                 break;
             case Association::belongsTo:
                 // nothing todo ...
                 break;
             case Association::hasMany:
                 $this->setAssocModelData($aModel, $aAssocModel, $aAssociation->fromKeys(), $aAssociation->toKeys());
                 if (!$aAssocModel->save()) {
                     return false;
                 }
                 break;
             case Association::hasAndBelongsToMany:
                 if (!$aAssocModel->save()) {
                     return false;
                 }
                 // -----------------------
                 // insert bridge table
                 foreach ($aAssocModel->childIterator() as $aOneChildModel) {
                     $this->buildBridge($aDB, $aAssociation, $aModel, $aOneChildModel);
                 }
                 break;
         }
     }
     return true;
 }
Example #4
0
 public function execute(DB $aDB, Model $aModel)
 {
     $aPrototype = $aModel->prototype();
     $aUpdate = $aPrototype->sharedStatementUpdate();
     // 从 belongs to model 中设置外键值
     foreach ($aPrototype->associations() as $aAssociation) {
         if ($aAssociation->isType(Association::belongsTo)) {
             if (!($aAssociatedModel = $aModel->child($aAssociation->name()))) {
                 continue;
             }
             if (!$aAssociatedModel->save()) {
                 return false;
             }
             $this->setAssociatedModelData($aAssociatedModel, $aModel, $aAssociation->toKeys(), $aAssociation->fromKeys());
         }
     }
     // 产生一个criteria并设置给$aUpdate
     //$aCriteria = StatementFactory::singleton()->createCriteria() ;
     //$aUpdate->setCriteria($aCriteria);
     // 处理主键
     $aUpdate->where()->clear();
     foreach ($aPrototype->keys() as $sKey) {
         //主键发生修改
         if ($aModel->changed($sKey)) {
             throw new ORMException('org\\jecat\\framework\\mvc\\model\\db\\orm\\Updater : Key 有修改,无法进行Update操作');
         } else {
             $aUpdate->where()->expression(array(SQL::createRawColumn(null, $sKey), '=', SQL::transValue($aModel->data($sKey))), true, true);
         }
     }
     $aUpdate->clearData();
     $bFlagChanged = false;
     //当前表是否有修改
     foreach ($aPrototype->columns() as $sClmName) {
         //只update发生修改的项
         if ($aModel->changed($sClmName)) {
             $aUpdate->setData($sClmName, $aModel->data($sClmName), false);
             $bFlagChanged = true;
         }
     }
     //只有当有修改的时候才发生更新
     if ($bFlagChanged) {
         $aDB->execute($aUpdate);
     }
     // update关联model
     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->save()) {
                     return false;
                 }
                 break;
             case Association::belongsTo:
                 // nothing todo ...
                 break;
             case Association::hasMany:
                 $this->setAssociatedModelData($aModel, $aAssociatedModel, $aAssociation->fromKeys(), $aAssociation->toKeys());
                 if (!$aAssociatedModel->save()) {
                     return false;
                 }
                 break;
             case Association::hasAndBelongsToMany:
                 if (!$aAssociatedModel->save()) {
                     return false;
                 }
                 // update bridge table
                 foreach ($aAssociatedModel->childIterator() as $aOneChildModel) {
                     $this->buildBridge($aDB, $aAssociation, $aModel, $aOneChildModel);
                 }
                 break;
         }
     }
     return true;
 }