public function setRelationMap(RelationMap $relationMap, $leftTableAlias = null, $relationAlias = null) { $leftCols = $relationMap->getLeftColumns(); $rightCols = $relationMap->getRightColumns(); $nbColumns = $relationMap->countColumnMappings(); for ($i = 0; $i < $nbColumns; $i++) { $this->addExplicitCondition($leftCols[$i]->getTableName(), $leftCols[$i]->getName(), $leftTableAlias, $rightCols[$i]->getTableName(), $rightCols[$i]->getName(), $relationAlias, Criteria::EQUAL); } $this->relationMap = $relationMap; return $this; }
public function setRelationMap(RelationMap $relationMap, $leftTableAlias = null, $relationAlias = null) { $leftCols = $relationMap->getLeftColumns(); $rightCols = $relationMap->getRightColumns(); $nbColumns = $relationMap->countColumnMappings(); for ($i = 0; $i < $nbColumns; $i++) { $leftColName = ($leftTableAlias ? $leftTableAlias : $leftCols[$i]->getTableName()) . '.' . $leftCols[$i]->getName(); $rightColName = ($relationAlias ? $relationAlias : $rightCols[$i]->getTableName()) . '.' . $rightCols[$i]->getName(); $this->addCondition($leftColName, $rightColName, Criteria::EQUAL); } $this->relationMap = $relationMap; $this->leftTableAlias = $leftTableAlias; $this->relationAlias = $relationAlias; return $this; }
/** * Method from the TableMap API. */ public function getRelations() { // table maps $authorTable = new \TableMap(); $authorTable->setClassName('\\Foo\\Author'); $resellerTable = new \TableMap(); $resellerTable->setClassName('\\Foo\\Reseller'); // relations $mainAuthorRelation = new \RelationMap('MainAuthor'); $mainAuthorRelation->setType(\RelationMap::MANY_TO_ONE); $mainAuthorRelation->setForeignTable($authorTable); $authorRelation = new \RelationMap('Author'); $authorRelation->setType(\RelationMap::ONE_TO_MANY); $authorRelation->setForeignTable($authorTable); $resellerRelation = new \RelationMap('Reseller'); $resellerRelation->setType(\RelationMap::MANY_TO_MANY); $resellerRelation->setLocalTable($resellerTable); return array($mainAuthorRelation, $authorRelation, $resellerRelation); }
/** * Adds a RelationMap to the table * * @param string $name The relation name * @param string $tablePhpName The related table name * @param integer $type The relation type (either RelationMap::MANY_TO_ONE, RelationMap::ONE_TO_MANY, or RelationMAp::ONE_TO_ONE) * @param array $columnMapping An associative array mapping column names (local => foreign) * @param string $onDelete SQL behavior upon deletion ('SET NULL', 'CASCADE', ...) * @param string $onUpdate SQL behavior upon update ('SET NULL', 'CASCADE', ...) * @param string $pluralName Optional plural name for *_TO_MANY relationships * @return RelationMap the built RelationMap object */ public function addRelation($name, $tablePhpName, $type, $columnMapping = array(), $onDelete = null, $onUpdate = null, $pluralName = null) { // note: using phpName for the second table allows the use of DatabaseMap::getTableByPhpName() // and this method autoloads the TableMap if the table isn't loaded yet $relation = new RelationMap($name); $relation->setType($type); $relation->setOnUpdate($onUpdate); $relation->setOnDelete($onDelete); if (null !== $pluralName) { $relation->setPluralName($pluralName); } // set tables if ($type == RelationMap::MANY_TO_ONE) { $relation->setLocalTable($this); $relation->setForeignTable($this->dbMap->getTableByPhpName($tablePhpName)); } else { $relation->setLocalTable($this->dbMap->getTableByPhpName($tablePhpName)); $relation->setForeignTable($this); $columnMapping = array_flip($columnMapping); } // set columns foreach ($columnMapping as $local => $foreign) { $relation->addColumnMapping( $relation->getLocalTable()->getColumn($local), $relation->getForeignTable()->getColumn($foreign) ); } $this->relations[$name] = $relation; return $relation; }
public function __toString() { return parent::toString() . ' tableMap: ' . ($this->tableMap ? get_class($this->tableMap) : 'null') . ' relationMap: ' . $this->relationMap->getName() . ' previousJoin: ' . ($this->previousJoin ? '(' . $this->previousJoin . ')' : 'null') . ' relationAlias: ' . $this->rightTableAlias; }
/** * Create array of options for relation. * * @param \RelationMap $relation * @return array */ public function getChoicesFromRelation(\RelationMap $relation) { $otherTable = $relation->getRightTable(); $objs = \PropelQuery::from($otherTable->getPhpName())->setFormatter(\ModelCriteria::FORMAT_ON_DEMAND)->find(); $opts = array(); foreach ($objs as $obj) { if (method_exists($obj, '__toString')) { $opts[$obj->getPrimaryKey()] = $obj->__toString(); } else { $opts[$obj->getPrimaryKey()] = $obj->getPrimaryKey(); } } return $opts; }
public function validFieldNameProvider() { $className = '\\Foo\\Book'; $options = array('foo' => 'bar'); // table maps $emptyTableMap = new \TableMap(); $authorTable = new \TableMap(); $authorTable->setClassName('\\Foo\\Author'); $resellerTable = new \TableMap(); $resellerTable->setClassName('\\Foo\\Reseller'); $relationsTableMap = $this->getMock('\\TableMap'); // relations $mainAuthorRelation = new \RelationMap('MainAuthor'); $mainAuthorRelation->setType(\RelationMap::MANY_TO_ONE); $mainAuthorRelation->setForeignTable($authorTable); $authorRelation = new \RelationMap('Author'); $authorRelation->setType(\RelationMap::ONE_TO_MANY); $authorRelation->setForeignTable($authorTable); $resellerRelation = new \RelationMap('Reseller'); $resellerRelation->setType(\RelationMap::MANY_TO_MANY); $resellerRelation->setLocalTable($resellerTable); // configure table maps mocks $relationsTableMap->expects($this->any())->method('getRelations')->will($this->returnValue(array($mainAuthorRelation, $authorRelation, $resellerRelation))); // columns $titleColumn = new \ColumnMap('Title', $emptyTableMap); $titleColumn->setType('text'); $titleColumn->setPhpName('Title'); $titleFieldMapping = array('id' => false, 'type' => 'text', 'fieldName' => 'Title'); return array(array(null, array(), $className, 'Title', $options, array('type' => null, 'association_mapping' => null, 'field_mapping' => null)), array($emptyTableMap, array(), $className, 'Title', $options, array('type' => null, 'association_mapping' => null, 'field_mapping' => null)), array($emptyTableMap, array($titleColumn), $className, 'Title', $options, array('type' => 'text', 'association_mapping' => null, 'field_mapping' => $titleFieldMapping)), array($relationsTableMap, array($titleColumn), $className, 'MainAuthor', $options, array('type' => \RelationMap::MANY_TO_ONE, 'association_mapping' => array('targetEntity' => '\\Foo\\Author', 'type' => \RelationMap::MANY_TO_ONE), 'field_mapping' => null)), array($relationsTableMap, array($titleColumn), $className, 'Authors', $options, array('type' => \RelationMap::ONE_TO_MANY, 'association_mapping' => array('targetEntity' => '\\Foo\\Author', 'type' => \RelationMap::ONE_TO_MANY), 'field_mapping' => null)), array($relationsTableMap, array($titleColumn), $className, 'Resellers', $options, array('type' => \RelationMap::MANY_TO_MANY, 'association_mapping' => array('targetEntity' => '\\Foo\\Reseller', 'type' => \RelationMap::MANY_TO_MANY), 'field_mapping' => null))); }