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); }
private function queryForMultitermAssoc(DB $aDB, Association $aMultitermAssoc, array &$arrDataSheet, $nRowIdx) { $aToPrototype = $aMultitermAssoc->toPrototype(); $aFromPrototype = $aMultitermAssoc->fromPrototype(); // 根据上一轮查询设置条件 if ($aMultitermAssoc->isType(Association::hasMany)) { $aRestraction = $this->makeResrictionForAsscotion($arrDataSheet[$nRowIdx], $aFromPrototype->path(), $aMultitermAssoc->fromKeys(), $aToPrototype->sqlTableAlias(), $aMultitermAssoc->toKeys()); } else { if ($aMultitermAssoc->isType(Association::hasAndBelongsToMany)) { $aRestraction = $this->makeResrictionForAsscotion($arrDataSheet[$nRowIdx], $aFromPrototype->path(), $aMultitermAssoc->fromKeys(), $aMultitermAssoc->bridgeSqlTableAlias(), $aMultitermAssoc->toBridgeKeys()); } else { throw new ORMException("what's this?"); } } // 新建的一个记录表 $sheet =& Model::dataSheet($arrDataSheet, $nRowIdx, $aToPrototype->name(), true); $this->execute($aToPrototype, $sheet, $aRestraction, array($aToPrototype->limitLength(), $aToPrototype->limitFrom()), $aDB); }