protected function buildBridge(DB $aDB, Association $aAssociation, Model $aFromModel, Model $aToModel) { $aFromPrototype = $aAssociation->fromPrototype(); $aSelect = new Select($aAssociation->bridgeTableName()); $aRestraction = $aSelect->criteria()->where(); $this->makeResrictionForAsscotion($aRestraction, $aFromPrototype->path(), $aAssociation->fromKeys(), null, $aAssociation->toBridgeKeys()); $this->makeResrictionForAsscotion($aRestraction, $aAssociation->toPrototype()->path(), $aAssociation->toKeys(), null, $aAssociation->fromBridgeKeys()); // 检查对应的桥接表记录是否存在 if (!$aDB->queryCount($aSelect)) { $aInsertForBridge = $aStatementFactory->createInsert($aAssociation->bridgeTableName()); // from table key vale $this->setValue($aInsertForBridge, $aAssociation->toBridgeKeys(), $aAssociation->fromKeys(), $aFromModel); // to table key vale $this->setValue($aInsertForBridge, $aAssociation->fromBridgeKeys(), $aAssociation->toKeys(), $aToModel); $aDB->execute($aInsertForBridge); } }
protected function deleteBridge(DB $aDB, Association $aAssociation, Model $aFromModel, Model $aToModel) { $aStatementFactory = $aAssociation->fromPrototype()->statementFactory(); $aDeleteForBridge = $aStatementFactory->createDelete($aAssociation->bridgeTableName()); $aDeleteForBridge->criteria()->where()->add($this->makeRestrictionForAssocotion($aFromModel, $aAssociation->fromKeys(), null, $aAssociation->toBridgeKeys(), $aStatementFactory)); $aDB->execute($aDeleteForBridge); }
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; }