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); }
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); } }
/** * $toTable 可以是一个字符串,也可以是一个Prototype对象,表示关联的表。 * @return Association */ public function createAssociation($nType, $to, $fromKeys = self::youKnow, $toKeys = self::youKnow, $sBridgeTable = null, $toBridgeKeys = self::youKnow, $fromBridgeKeys = self::youKnow) { if (is_string($to)) { $aToPrototype = self::create($to, self::youKnow, '*', $this->db()); } else { if ($to instanceof Prototype) { $aToPrototype = $to; if ($aToPrototype->aAssociatedBy !== null) { throw new Exception('函数 Prototype::createAssociation() 的参数 $to 已经被关联,不能再关联到其他原型'); } } else { throw new Exception('函数 Prototype::createAssociation() 的参数 $to 必须是数据表名称或Prototype对象'); } } $aAsso = new Association($this->db(), $nType, $this, $aToPrototype, $fromKeys, $toKeys, $sBridgeTable, $toBridgeKeys, $fromBridgeKeys); $this->arrAssociations[] = $aAsso; $aToPrototype->aAssociatedBy = $aAsso; return $aAsso->toPrototype(); }