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 totalCount(DB $aDB, Prototype $aPrototype, Restriction $aRestriction = null) { $aSelect = $aPrototype->sharedStatementSelect(); $aCriteria = $aSelect->criteria(); $aCriteria->setLimit(-1); if ($aRestriction) { $aSelect->criteria()->where()->add($aRestriction); } try { $nTotalCount = $aDB->queryCount($aSelect, "*", Prototype::sqlCompiler()); } catch (\Exception $e) { } //} final { if ($aRestriction) { $aSelect->criteria()->where()->remove($aRestriction); } //} if (!empty($e)) { throw $e; } return $nTotalCount; }