123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- <?php
- class RelationMap
- {
- const
-
- MANY_TO_ONE = 1,
- ONE_TO_MANY = 2,
- ONE_TO_ONE = 3,
- MANY_TO_MANY = 4,
-
- LOCAL_TO_FOREIGN = 0,
- LEFT_TO_RIGHT = 1;
-
- protected
- $name,
- $type,
- $localTable,
- $foreignTable,
- $localColumns = array(),
- $foreignColumns = array(),
- $onUpdate, $onDelete;
-
- public function __construct($name)
- {
- $this->name = $name;
- }
-
-
- public function getName()
- {
- return $this->name;
- }
-
- public function setType($type)
- {
- $this->type = $type;
- }
-
- public function getType()
- {
- return $this->type;
- }
-
- public function setLocalTable($table)
- {
- $this->localTable = $table;
- }
-
- public function getLocalTable()
- {
- return $this->localTable;
- }
-
- public function setForeignTable($table)
- {
- $this->foreignTable = $table;
- }
-
- public function getForeignTable()
- {
- return $this->foreignTable;
- }
-
-
- public function getLeftTable()
- {
- return ($this->getType() == RelationMap::MANY_TO_ONE) ? $this->getLocalTable() : $this->getForeignTable();
- }
-
- public function getRightTable()
- {
- return ($this->getType() == RelationMap::MANY_TO_ONE) ? $this->getForeignTable() : $this->getLocalTable();
- }
-
-
- public function addColumnMapping(ColumnMap $local, ColumnMap $foreign)
- {
- $this->localColumns[] = $local;
- $this->foreignColumns[] = $foreign;
- }
-
-
- public function getColumnMappings($direction = RelationMap::LOCAL_TO_FOREIGN)
- {
- $h = array();
- if ($direction == RelationMap::LEFT_TO_RIGHT && $this->getType() == RelationMap::MANY_TO_ONE) {
- $direction = RelationMap::LOCAL_TO_FOREIGN;
- }
- for ($i=0, $size=count($this->localColumns); $i < $size; $i++) {
- if ($direction == RelationMap::LOCAL_TO_FOREIGN) {
- $h[$this->localColumns[$i]->getFullyQualifiedName()] = $this->foreignColumns[$i]->getFullyQualifiedName();
- } else {
- $h[$this->foreignColumns[$i]->getFullyQualifiedName()] = $this->localColumns[$i]->getFullyQualifiedName();
- }
- }
- return $h;
- }
-
-
- public function isComposite()
- {
- return $this->countColumnMappings() > 1;
- }
-
-
- public function countColumnMappings()
- {
- return count($this->localColumns);
- }
-
-
- public function getLocalColumns()
- {
- return $this->localColumns;
- }
-
-
- public function getForeignColumns()
- {
- return $this->foreignColumns;
- }
-
-
- public function getLeftColumns()
- {
- return ($this->getType() == RelationMap::MANY_TO_ONE) ? $this->getLocalColumns() : $this->getForeignColumns();
- }
-
- public function getRightColumns()
- {
- return ($this->getType() == RelationMap::MANY_TO_ONE) ? $this->getForeignColumns() : $this->getLocalColumns();
- }
-
- public function setOnUpdate($onUpdate)
- {
- $this->onUpdate = $onUpdate;
- }
-
- public function getOnUpdate()
- {
- return $this->onUpdate;
- }
-
-
- public function setOnDelete($onDelete)
- {
- $this->onDelete = $onDelete;
- }
-
- public function getOnDelete()
- {
- return $this->onDelete;
- }
-
-
- public function getSymmetricalRelation()
- {
- $localMapping = array($this->getLeftColumns(), $this->getRightColumns());
- foreach ($this->getRightTable()->getRelations() as $relation) {
- if ($localMapping == array($relation->getRightColumns(), $relation->getLeftColumns())) {
- return $relation;
- }
- }
- }
- }
|