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); } }
public function queryCount(Select $aSelect, $sColumn = '*', SqlCompiler $aSqlCompiler = null) { $arrRawSelect =& $aSelect->rawClause(SQL::CLAUSE_SELECT); $arrReturnsBak =& $arrRawSelect['subtree']; $arrTmp = array("count({$sColumn}) as rowCount"); $arrRawSelect['subtree'] =& $arrTmp; try { $aRecords = $this->query($aSelect, null, $aSqlCompiler); } catch (\Exception $e) { } //} final { $arrRawSelect['subtree'] =& $arrReturnsBak; //} if (isset($e)) { throw $e; } if ($aRecords) { $arrRow = $aRecords->fetch(\PDO::FETCH_ASSOC); return intval($arrRow['rowCount']); } else { return 0; } }