blob: 795630abefc0a63e0e5268e02c4d0fa9a07ecac7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
<?php
/**
* TActiveRecordHasMany class file.
*
* @author Wei Zhuo <weizhuo[at]gmail[dot]com>
* @link http://www.pradosoft.com/
* @copyright Copyright © 2005-2007 PradoSoft
* @license http://www.pradosoft.com/license/
* @version $Id$
* @package System.Data.ActiveRecord.Relations
*/
/**
* Loads base active record relations class.
*/
Prado::using('System.Data.ActiveRecord.Relations.TActiveRecordRelation');
/**
* Implements TActiveRecord::HAS_MANY relationship between the source object having zero or
* more foreign objects. Consider the <b>entity</b> relationship between a Team and a Player.
* <code>
* +------+ +--------+
* | Team | 1 <----- * | Player |
* +------+ +--------+
* </code>
* Where one team may have 0 or more players and each player belongs to only
* one team. We may model Team-Player <b>object</b> relationship as active record as follows.
* <code>
* class TeamRecord extends TActiveRecord
* {
* const TABLE='team';
* public $name; //primary key
* public $location;
*
* public $players=array(); //list of players
*
* protected static $RELATIONS=array(
* 'players' => array(self::HAS_MANY, 'PlayerRecord'));
*
* public static function finder($className=__CLASS__)
* {
* return parent::finder($className);
* }
* }
* class PlayerRecord extends TActiveRecord
* {
* // see TActiveRecordBelongsTo for detailed definition
* }
* </code>
* The <tt>$RELATIONS</tt> static property of TeamRecord defines that the
* property <tt>$players</tt> has many <tt>PlayerRecord</tt>s.
*
* The players list may be fetched as follows.
* <code>
* $team = TeamRecord::finder()->with_players()->findAll();
* </code>
* The method <tt>with_xxx()</tt> (where <tt>xxx</tt> is the relationship property
* name, in this case, <tt>players</tt>) fetchs the corresponding PlayerRecords using
* a second query (not by using a join). The <tt>with_xxx()</tt> accepts the same
* arguments as other finder methods of TActiveRecord, e.g. <tt>with_player('age < ?', 35)</tt>.
*
* @author Wei Zhuo <weizho[at]gmail[dot]com>
* @version $Id$
* @package System.Data.ActiveRecord.Relations
* @since 3.1
*/
class TActiveRecordHasMany extends TActiveRecordRelation
{
/**
* Get the foreign key index values from the results and make calls to the
* database to find the corresponding foreign objects.
* @param array original results.
*/
protected function collectForeignObjects(&$results)
{
$fkObject = $this->getContext()->getForeignRecordFinder();
$fkeys = $this->findForeignKeys($fkObject, $this->getSourceRecord());
$properties = array_values($fkeys);
$fields = array_keys($fkeys);
$indexValues = $this->getIndexValues($properties, $results);
$fkObjects = $this->findForeignObjects($fields,$indexValues);
$this->populateResult($results,$properties,$fkObjects,$fields);
}
}
?>
|