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;
 }
Ejemplo n.º 2
0
 /**
  *
  * @param Model $aModel 子分类的model
  * @return ModelList 所有子分类的model,排序高级分类在前,子分类在后
  */
 public static function getChildren(Model $aModel)
 {
     if (!$aModel) {
         return;
     }
     $aPrototype = clone $aModel->prototype();
     $aPrototype->addOrderBy('lft');
     $aParentsModelList = $aPrototype->createModel(true);
     $aParentsModelList->loadSql("lft < @1 and rgt > @2", $aModel->lft, $aModel->rgt);
     return $aParentsModelList;
 }
Ejemplo n.º 3
0
 public function hasExists(Model $aModel, Prototype $aPrototype = null, Select $aSelect = null, DB $aDB = null)
 {
     if (!$aPrototype) {
         $aPrototype = $aModel->prototype();
     }
     if (!$aSelect) {
         $aSelect = $aPrototype->sharedStatementSelect();
     }
     if (!$aDB) {
         $aDB = DB::singleton();
     }
     // 备份原来的 limit
     $arrRawOriLimit =& $aSelect->rawClause(SQL::CLAUSE_LIMIT, false);
     $aSelect->criteria()->setLimit(1, 0);
     $aRestriction = $aSelect->where()->createRestriction();
     foreach ($aPrototype->keys() as $nIdx => $sKey) {
         $aRestriction->expression(array(SQL::createRawColumn($aPrototype->sqlTableAlias(), $sKey), '=', SQL::transValue($aModel->data($sKey))), true, true);
     }
     // 查询
     $bExists = $aDB->queryCount($aSelect) > 0;
     // 移除条件
     $aSelect->where()->remove($aRestriction);
     // 还原原来的 limit
     $aSelect->setRawClause(SQL::CLAUSE_LIMIT, $arrRawOriLimit);
     return $bExists;
 }
Ejemplo n.º 4
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;
 }
Ejemplo n.º 5
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;
 }