Beispiel #1
0
 protected function makeResrictionForAsscotion(array &$arrDataRow, $sFromTableName = null, array $arrFromKeys, $sToTableName = null, array $arrToKeys)
 {
     $aRestriction = new Restriction();
     foreach ($arrFromKeys as $nIdx => $sFromKey) {
         if ($sFromTableName) {
             $sFromKey = $sFromTableName . '.' . $sFromKey;
         }
         $aRestriction->expression(array(SQL::createRawColumn($sToTableName, $arrToKeys[$nIdx]), '=', SQL::transValue($arrDataRow[$sFromKey])), true, true);
     }
     return $aRestriction;
 }
Beispiel #2
0
 public function setData($sColumnName, $value = null, $bValueExpr = false)
 {
     list($sTableName, $sColumnName) = SQL::splitColumn($sColumnName);
     $arrRawSet =& $this->rawClause(SQL::CLAUSE_SET);
     if (!isset($arrRawSet['subtree'][$sColumnName]) and !empty($arrRawSet['subtree'])) {
         $arrRawSet['subtree'][] = ',';
     }
     $arrRawSet['subtree'][$sColumnName] = array('expr_type' => 'assignment', 'pretree' => array(SQL::createRawColumn($sTableName, $sColumnName), '='));
     if ($bValueExpr) {
         $arrRawSet['subtree'][$sColumnName]['subtree'] =& SQL::parseSql($value, 'values', true);
     } else {
         $arrRawSet['subtree'][$sColumnName]['subtree'] = array(SQL::transValue($value));
     }
 }
Beispiel #3
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;
 }
Beispiel #4
0
 private static function &makeResrictionForForeignKey($sFromTableName = null, $sToTableName = null, array &$arrFromKeys, array &$arrToKeys, array &$arrConditions = null)
 {
     $arrTokens = array('ON', '(');
     foreach ($arrFromKeys as $nIdx => $sFromKey) {
         if ($nIdx) {
             $arrTokens[] = 'AND';
         }
         $arrTokens[] = SQL::createRawColumn($sFromTableName, $sFromKey);
         $arrTokens[] = '=';
         $arrTokens[] = SQL::createRawColumn($sToTableName, $arrToKeys[$nIdx]);
     }
     if ($arrConditions) {
         $arrTokens[] = 'AND';
         $arrTokens[] = $arrConditions;
     }
     $arrTokens[] = ')';
     return $arrTokens;
 }
Beispiel #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;
 }
Beispiel #6
0
 public static function buildRestriction(Prototype $aPrototype, $values = null, $keys = null)
 {
     if ($values === null) {
         return null;
     }
     $keys = $keys ? (array) $keys : $aPrototype->keys();
     if ($values instanceof Restriction) {
         return $values;
     } else {
         $aRestriction = new Restriction();
         $sSqlTableAlias = $aPrototype->sqlTableAlias();
         $values = array_values((array) $values);
         foreach ($keys as $nIdx => $sKey) {
             list($sTable, $sColumn) = SQL::splitColumn($sKey);
             $aRestriction->expression(array(SQL::createRawColumn($sTable, $sColumn), '=', SQL::transValue($values[$nIdx])), true, true);
         }
         return $aRestriction;
     }
 }