3 * TActiveRecordHasManyAssociation class file.
5 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6 * @link http://www.pradosoft.com/
7 * @copyright Copyright © 2005-2014 PradoSoft
8 * @license http://www.pradosoft.com/license/
10 * @package System.Data.ActiveRecord.Relations
14 * Loads base active record relations class.
16 Prado::using('System.Data.ActiveRecord.Relations.TActiveRecordRelation');
19 * Implements the M-N (many to many) relationship via association table.
20 * Consider the <b>entity</b> relationship between Articles and Categories
21 * via the association table <tt>Article_Category</tt>.
23 * +---------+ +------------------+ +----------+
24 * | Article | * -----> * | Article_Category | * <----- * | Category |
25 * +---------+ +------------------+ +----------+
27 * Where one article may have 0 or more categories and each category may have 0
28 * or more articles. We may model Article-Category <b>object</b> relationship
29 * as active record as follows.
33 * const TABLE='Article';
36 * public $Categories=array(); //foreign object collection.
38 * public static $RELATIONS = array
40 * 'Categories' => array(self::MANY_TO_MANY, 'CategoryRecord', 'Article_Category')
43 * public static function finder($className=__CLASS__)
45 * return parent::finder($className);
48 * class CategoryRecord
50 * const TABLE='Category';
51 * public $category_id;
53 * public $Articles=array();
55 * public static $RELATIONS = array
57 * 'Articles' => array(self::MANY_TO_MANY, 'ArticleRecord', 'Article_Category')
60 * public static function finder($className=__CLASS__)
62 * return parent::finder($className);
67 * The static <tt>$RELATIONS</tt> property of ArticleRecord defines that the
68 * property <tt>$Categories</tt> has many <tt>CategoryRecord</tt>s. Similar, the
69 * static <tt>$RELATIONS</tt> property of CategoryRecord defines many ArticleRecords.
71 * The articles with categories list may be fetched as follows.
73 * $articles = TeamRecord::finder()->withCategories()->findAll();
75 * The method <tt>with_xxx()</tt> (where <tt>xxx</tt> is the relationship property
76 * name, in this case, <tt>Categories</tt>) fetchs the corresponding CategoryRecords using
77 * a second query (not by using a join). The <tt>with_xxx()</tt> accepts the same
78 * arguments as other finder methods of TActiveRecord.
80 * @author Wei Zhuo <weizho[at]gmail[dot]com>
82 * @package System.Data.ActiveRecord.Relations
85 class TActiveRecordHasManyAssociation extends TActiveRecordRelation
87 private $_association;
88 private $_sourceTable;
89 private $_foreignTable;
90 private $_association_columns=array();
93 * Get the foreign key index values from the results and make calls to the
94 * database to find the corresponding foreign objects using association table.
95 * @param array original results.
97 protected function collectForeignObjects(&$results)
99 list($sourceKeys, $foreignKeys) = $this->getRelationForeignKeys();
100 $properties = array_values($sourceKeys);
101 $indexValues = $this->getIndexValues($properties, $results);
102 $this->fetchForeignObjects($results, $foreignKeys,$indexValues,$sourceKeys);
106 * @return array 2 arrays of source keys and foreign keys from the association table.
108 public function getRelationForeignKeys()
110 $association = $this->getAssociationTable();
111 $sourceKeys = $this->findForeignKeys($association, $this->getSourceRecord(), true);
112 $fkObject = $this->getContext()->getForeignRecordFinder();
113 $foreignKeys = $this->findForeignKeys($association, $fkObject);
114 return array($sourceKeys, $foreignKeys);
118 * @return TDbTableInfo association table information.
120 protected function getAssociationTable()
122 if($this->_association===null)
124 $gateway = $this->getSourceRecord()->getRecordGateway();
125 $conn = $this->getSourceRecord()->getDbConnection();
126 //table name may include the fk column name separated with a dot.
127 $table = explode('.', $this->getContext()->getAssociationTable());
130 $columns = preg_replace('/^\((.*)\)/', '\1', $table[1]);
131 $this->_association_columns = preg_split('/\s*[, ]\*/',$columns);
133 $this->_association = $gateway->getTableInfo($conn, $table[0]);
135 return $this->_association;
139 * @return TDbTableInfo source table information.
141 protected function getSourceTable()
143 if($this->_sourceTable===null)
145 $gateway = $this->getSourceRecord()->getRecordGateway();
146 $this->_sourceTable = $gateway->getRecordTableInfo($this->getSourceRecord());
148 return $this->_sourceTable;
152 * @return TDbTableInfo foreign table information.
154 protected function getForeignTable()
156 if($this->_foreignTable===null)
158 $gateway = $this->getSourceRecord()->getRecordGateway();
159 $fkObject = $this->getContext()->getForeignRecordFinder();
160 $this->_foreignTable = $gateway->getRecordTableInfo($fkObject);
162 return $this->_foreignTable;
166 * @return TDataGatewayCommand
168 protected function getCommandBuilder()
170 return $this->getSourceRecord()->getRecordGateway()->getCommand($this->getSourceRecord());
174 * @return TDataGatewayCommand
176 protected function getForeignCommandBuilder()
178 $obj = $this->getContext()->getForeignRecordFinder();
179 return $this->getSourceRecord()->getRecordGateway()->getCommand($obj);
184 * Fetches the foreign objects using TActiveRecord::findAllByIndex()
185 * @param array field names
186 * @param array foreign key index values.
188 protected function fetchForeignObjects(&$results,$foreignKeys,$indexValues,$sourceKeys)
190 $criteria = $this->getCriteria();
191 $finder = $this->getContext()->getForeignRecordFinder();
192 $type = get_class($finder);
193 $command = $this->createCommand($criteria, $foreignKeys,$indexValues,$sourceKeys);
194 $srcProps = array_keys($sourceKeys);
195 $collections=array();
196 foreach($this->getCommandBuilder()->onExecuteCommand($command, $command->query()) as $row)
198 $hash = $this->getObjectHash($row, $srcProps);
199 foreach($srcProps as $column)
200 unset($row[$column]);
201 $obj = $this->createFkObject($type,$row,$foreignKeys);
202 $collections[$hash][] = $obj;
204 $this->setResultCollection($results, $collections, array_values($sourceKeys));
208 * @param string active record class name.
209 * @param array row data
210 * @param array foreign key column names
211 * @return TActiveRecord
213 protected function createFkObject($type,$row,$foreignKeys)
215 $obj = TActiveRecord::createRecord($type, $row);
216 if(count($this->_association_columns) > 0)
219 foreach($foreignKeys as $ref=>$fk)
220 $obj->setColumnValue($ref, $row[$this->_association_columns[$i++]]);
226 * @param TSqlCriteria
227 * @param TTableInfo association table info
228 * @param array field names
229 * @param array field values
231 public function createCommand($criteria, $foreignKeys,$indexValues,$sourceKeys)
233 $innerJoin = $this->getAssociationJoin($foreignKeys,$indexValues,$sourceKeys);
234 $fkTable = $this->getForeignTable()->getTableFullName();
235 $srcColumns = $this->getSourceColumns($sourceKeys);
236 if(($where=$criteria->getCondition())===null)
238 $sql = "SELECT {$fkTable}.*, {$srcColumns} FROM {$fkTable} {$innerJoin} WHERE {$where}";
240 $parameters = $criteria->getParameters()->toArray();
241 $ordering = $criteria->getOrdersBy();
242 $limit = $criteria->getLimit();
243 $offset = $criteria->getOffset();
245 $builder = $this->getForeignCommandBuilder()->getBuilder();
246 $command = $builder->applyCriterias($sql,$parameters,$ordering,$limit,$offset);
247 $this->getCommandBuilder()->onCreateCommand($command, $criteria);
252 * @param array source table column names.
253 * @return string comma separated source column names.
255 protected function getSourceColumns($sourceKeys)
258 $table = $this->getAssociationTable();
259 $tableName = $table->getTableFullName();
260 $columnNames = array_merge(array_keys($sourceKeys),$this->_association_columns);
261 foreach($columnNames as $name)
262 $columns[] = $tableName.'.'.$table->getColumn($name)->getColumnName();
263 return implode(', ', $columns);
267 * SQL inner join for M-N relationship via association table.
268 * @param array foreign table column key names.
269 * @param array source table index values.
270 * @param array source table column names.
271 * @return string inner join condition for M-N relationship via association table.
273 protected function getAssociationJoin($foreignKeys,$indexValues,$sourceKeys)
275 $refInfo= $this->getAssociationTable();
276 $fkInfo = $this->getForeignTable();
278 $refTable = $refInfo->getTableFullName();
279 $fkTable = $fkInfo->getTableFullName();
282 $hasAssociationColumns = count($this->_association_columns) > 0;
284 foreach($foreignKeys as $ref=>$fk)
286 if($hasAssociationColumns)
287 $refField = $refInfo->getColumn($this->_association_columns[$i++])->getColumnName();
289 $refField = $refInfo->getColumn($ref)->getColumnName();
290 $fkField = $fkInfo->getColumn($fk)->getColumnName();
291 $joins[] = "{$fkTable}.{$fkField} = {$refTable}.{$refField}";
293 $joinCondition = implode(' AND ', $joins);
294 $index = $this->getCommandBuilder()->getIndexKeyCondition($refInfo,array_keys($sourceKeys), $indexValues);
295 return "INNER JOIN {$refTable} ON ({$joinCondition}) AND {$index}";
299 * Updates the associated foreign objects.
300 * @return boolean true if all update are success (including if no update was required), false otherwise .
302 public function updateAssociatedRecords()
304 $obj = $this->getContext()->getSourceRecord();
305 $fkObjects = &$obj->{$this->getContext()->getProperty()};
307 if(($total = count($fkObjects))> 0)
309 $source = $this->getSourceRecord();
310 $builder = $this->getAssociationTableCommandBuilder();
311 for($i=0;$i<$total;$i++)
312 $success = $fkObjects[$i]->save() && $success;
313 return $this->updateAssociationTable($obj, $fkObjects, $builder) && $success;
319 * @return TDbCommandBuilder
321 protected function getAssociationTableCommandBuilder()
323 $conn = $this->getContext()->getSourceRecord()->getDbConnection();
324 return $this->getAssociationTable()->createCommandBuilder($conn);
327 private function hasAssociationData($builder,$data)
330 $table = $this->getAssociationTable();
331 foreach($data as $name=>$value)
332 $condition[] = $table->getColumn($name)->getColumnName().' = ?';
333 $command = $builder->createCountCommand(implode(' AND ', $condition),array_values($data));
334 $result = $this->getCommandBuilder()->onExecuteCommand($command, intval($command->queryScalar()));
335 return intval($result) > 0;
338 private function addAssociationData($builder,$data)
340 $command = $builder->createInsertCommand($data);
341 return $this->getCommandBuilder()->onExecuteCommand($command, $command->execute()) > 0;
344 private function updateAssociationTable($obj,$fkObjects, $builder)
346 $source = $this->getSourceRecordValues($obj);
347 $foreignKeys = $this->findForeignKeys($this->getAssociationTable(), $fkObjects[0]);
349 foreach($fkObjects as $fkObject)
351 $data = array_merge($source, $this->getForeignObjectValues($foreignKeys,$fkObject));
352 if(!$this->hasAssociationData($builder,$data))
353 $success = $this->addAssociationData($builder,$data) && $success;
358 private function getSourceRecordValues($obj)
360 $sourceKeys = $this->findForeignKeys($this->getAssociationTable(), $obj);
361 $indexValues = $this->getIndexValues(array_values($sourceKeys), $obj);
364 foreach($sourceKeys as $name=>$srcKey)
365 $data[$name] = $indexValues[0][$i++];
369 private function getForeignObjectValues($foreignKeys,$fkObject)
372 foreach($foreignKeys as $name=>$fKey)
373 $data[$name] = $fkObject->getColumnValue($fKey);