| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TActiveRecordHasMany class file.
|
|
|
4 |
*
|
|
|
5 |
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
|
|
|
6 |
* @link http://www.pradosoft.com/
|
|
|
7 |
* @copyright Copyright © 2005-2008 PradoSoft
|
|
|
8 |
* @license http://www.pradosoft.com/license/
|
|
|
9 |
* @version $Id$
|
|
|
10 |
* @package System.Data.ActiveRecord.Relations
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
/**
|
|
|
14 |
* Loads base active record relations class.
|
|
|
15 |
*/
|
|
|
16 |
Prado::using('System.Data.ActiveRecord.Relations.TActiveRecordRelation');
|
|
|
17 |
|
|
|
18 |
/**
|
|
|
19 |
* Implements TActiveRecord::HAS_MANY relationship between the source object having zero or
|
|
|
20 |
* more foreign objects. Consider the <b>entity</b> relationship between a Team and a Player.
|
|
|
21 |
* <code>
|
|
|
22 |
* +------+ +--------+
|
|
|
23 |
* | Team | 1 <----- * | Player |
|
|
|
24 |
* +------+ +--------+
|
|
|
25 |
* </code>
|
|
|
26 |
* Where one team may have 0 or more players and each player belongs to only
|
|
|
27 |
* one team. We may model Team-Player <b>object</b> relationship as active record as follows.
|
|
|
28 |
* <code>
|
|
|
29 |
* class TeamRecord extends TActiveRecord
|
|
|
30 |
* {
|
|
|
31 |
* const TABLE='team';
|
|
|
32 |
* public $name; //primary key
|
|
|
33 |
* public $location;
|
|
|
34 |
*
|
|
|
35 |
* public $players=array(); //list of players
|
|
|
36 |
*
|
|
|
37 |
* public static $RELATIONS=array
|
|
|
38 |
* (
|
|
|
39 |
* 'players' => array(self::HAS_MANY, 'PlayerRecord')
|
|
|
40 |
* );
|
|
|
41 |
*
|
|
|
42 |
* public static function finder($className=__CLASS__)
|
|
|
43 |
* {
|
|
|
44 |
* return parent::finder($className);
|
|
|
45 |
* }
|
|
|
46 |
* }
|
|
|
47 |
* class PlayerRecord extends TActiveRecord
|
|
|
48 |
* {
|
|
|
49 |
* // see TActiveRecordBelongsTo for detailed definition
|
|
|
50 |
* }
|
|
|
51 |
* </code>
|
|
|
52 |
* The static <tt>$RELATIONS</tt> property of TeamRecord defines that the
|
|
|
53 |
* property <tt>$players</tt> has many <tt>PlayerRecord</tt>s.
|
|
|
54 |
*
|
|
|
55 |
* The players list may be fetched as follows.
|
|
|
56 |
* <code>
|
|
|
57 |
* $team = TeamRecord::finder()->with_players()->findAll();
|
|
|
58 |
* </code>
|
|
|
59 |
* The method <tt>with_xxx()</tt> (where <tt>xxx</tt> is the relationship property
|
|
|
60 |
* name, in this case, <tt>players</tt>) fetchs the corresponding PlayerRecords using
|
|
|
61 |
* a second query (not by using a join). The <tt>with_xxx()</tt> accepts the same
|
|
|
62 |
* arguments as other finder methods of TActiveRecord, e.g. <tt>with_players('age < ?', 35)</tt>.
|
|
|
63 |
*
|
|
|
64 |
* @author Wei Zhuo <weizho[at]gmail[dot]com>
|
|
|
65 |
* @version $Id$
|
|
|
66 |
* @package System.Data.ActiveRecord.Relations
|
|
|
67 |
* @since 3.1
|
|
|
68 |
*/
|
|
|
69 |
class TActiveRecordHasMany extends TActiveRecordRelation
|
|
|
70 |
{
|
|
|
71 |
/**
|
|
|
72 |
* Get the foreign key index values from the results and make calls to the
|
|
|
73 |
* database to find the corresponding foreign objects.
|
|
|
74 |
* @param array original results.
|
|
|
75 |
*/
|
|
|
76 |
protected function collectForeignObjects(&$results)
|
|
|
77 |
{
|
|
|
78 |
$fkeys = $this->getRelationForeignKeys();
|
|
|
79 |
|
|
|
80 |
$properties = array_values($fkeys);
|
|
|
81 |
$fields = array_keys($fkeys);
|
|
|
82 |
|
|
|
83 |
$indexValues = $this->getIndexValues($properties, $results);
|
|
|
84 |
$fkObjects = $this->findForeignObjects($fields,$indexValues);
|
|
|
85 |
$this->populateResult($results,$properties,$fkObjects,$fields);
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* @return array foreign key field names as key and object properties as value.
|
|
|
90 |
* @since 3.1.2
|
|
|
91 |
*/
|
|
|
92 |
public function getRelationForeignKeys()
|
|
|
93 |
{
|
|
|
94 |
$fkObject = $this->getContext()->getForeignRecordFinder();
|
|
|
95 |
return $this->findForeignKeys($fkObject, $this->getSourceRecord());
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
/**
|
|
|
99 |
* Updates the associated foreign objects.
|
|
|
100 |
* @return boolean true if all update are success (including if no update was required), false otherwise .
|
|
|
101 |
*/
|
|
|
102 |
public function updateAssociatedRecords()
|
|
|
103 |
{
|
|
|
104 |
$obj = $this->getContext()->getSourceRecord();
|
|
|
105 |
$fkObjects = &$obj->{$this->getContext()->getProperty()};
|
|
|
106 |
$success=true;
|
|
|
107 |
if(($total = count($fkObjects))> 0)
|
|
|
108 |
{
|
|
|
109 |
$source = $this->getSourceRecord();
|
|
|
110 |
$fkeys = $this->findForeignKeys($fkObjects[0], $source);
|
|
|
111 |
for($i=0;$i<$total;$i++)
|
|
|
112 |
{
|
|
|
113 |
foreach($fkeys as $fKey => $srcKey)
|
|
|
114 |
$fkObjects[$i]->setColumnValue($fKey, $source->getColumnValue($srcKey));
|
|
|
115 |
$success = $fkObjects[$i]->save() && $success;
|
|
|
116 |
}
|
|
|
117 |
}
|
|
|
118 |
return $success;
|
|
|
119 |
}
|
|
|
120 |
}
|
|
|
121 |
|