Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TActiveRecordHasOne class file.
4
 *
5
 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
6
 * @link http://www.pradosoft.com/
7
 * @copyright Copyright &copy; 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 relationship class.
15
 */
16
Prado::using('System.Data.ActiveRecord.Relations.TActiveRecordRelation');
17
 
18
/**
19
 * TActiveRecordHasOne models the object relationship that a record (the source object)
20
 * property is an instance of foreign record object having a foreign key
21
 * related to the source object. The HAS_ONE relation is very similar to the
22
 * HAS_MANY relationship (in fact, it is equivalent in the entities relationship point of view).
23
 *
24
 * The difference of HAS_ONE from HAS_MANY is that the foreign object is singular.
25
 * That is, HAS_MANY will return a collection of records while HAS_ONE returns the
26
 * corresponding record.
27
 *
28
 * Consider the <b>entity</b> relationship between a Car and a Engine.
29
 * <code>
30
 * +-----+            +--------+
31
 * | Car | 1 <----- 1 | Engine |
32
 * +-----+            +--------+
33
 * </code>
34
 * Where each engine belongs to only one car, that is, the Engine entity has
35
 * a foreign key to the Car's primary key. We may model
36
 * Engine-Car <b>object</b> relationship as active record as follows.
37
 * <code>
38
 * class CarRecord extends TActiveRecord
39
 * {
40
 *     const TABLE='car';
41
 *     public $car_id; //primary key
42
 *     public $colour;
43
 *
44
 *     public $engine; //engine foreign object
45
 *
46
 *     public static $RELATIONS=array
47
 *     (
48
 *         'engine' => array(self::HAS_ONE, 'EngineRecord')
49
 *     );
50
 *
51
 *	   public static function finder($className=__CLASS__)
52
 *	   {
53
 *		   return parent::finder($className);
54
 *	   }
55
 * }
56
 * class EngineRecord extends TActiveRecord
57
 * {
58
 *     const TABLE='engine';
59
 *     public $engine_id;
60
 *     public $capacity;
61
 *     public $car_id; //foreign key to cars
62
 *
63
 *	   public static function finder($className=__CLASS__)
64
 *	   {
65
 *		   return parent::finder($className);
66
 *	   }
67
 * }
68
 * </code>
69
 * The static <tt>$RELATIONS</tt> property of CarRecord defines that the
70
 * property <tt>$engine</tt> that will reference an <tt>EngineRecord</tt> instance.
71
 *
72
 * The car record with engine property list may be fetched as follows.
73
 * <code>
74
 * $cars = CarRecord::finder()->with_engine()->findAll();
75
 * </code>
76
 * The method <tt>with_xxx()</tt> (where <tt>xxx</tt> is the relationship property
77
 * name, in this case, <tt>engine</tt>) fetchs the corresponding EngineRecords using
78
 * a second query (not by using a join). The <tt>with_xxx()</tt> accepts the same
79
 * arguments as other finder methods of TActiveRecord, e.g. <tt>with_engine('capacity < ?', 3.8)</tt>.
80
 *
81
 * @author Wei Zhuo <weizho[at]gmail[dot]com>
82
 * @version $Id$
83
 * @package System.Data.ActiveRecord.Relations
84
 * @since 3.1
85
 */
86
class TActiveRecordHasOne extends TActiveRecordRelation
87
{
88
	/**
89
	 * Get the foreign key index values from the results and make calls to the
90
	 * database to find the corresponding foreign objects.
91
	 * @param array original results.
92
	 */
93
	protected function collectForeignObjects(&$results)
94
	{
95
		$fkeys = $this->getRelationForeignKeys();
96
		$properties = array_values($fkeys);
97
		$fields = array_keys($fkeys);
98
 
99
		$indexValues = $this->getIndexValues($properties, $results);
100
		$fkObjects = $this->findForeignObjects($fields,$indexValues);
101
		$this->populateResult($results,$properties,$fkObjects,$fields);
102
	}
103
 
104
	/**
105
	 * @return array foreign key field names as key and object properties as value.
106
	 * @since 3.1.2
107
	 */
108
	public function getRelationForeignKeys()
109
	{
110
		$fkObject = $this->getContext()->getForeignRecordFinder();
111
		return $this->findForeignKeys($fkObject, $this->getSourceRecord());
112
	}
113
 
114
	/**
115
	 * Sets the foreign objects to the given property on the source object.
116
	 * @param TActiveRecord source object.
117
	 * @param array foreign objects.
118
	 */
119
	protected function setObjectProperty($source, $properties, &$collections)
120
	{
121
		$hash = $this->getObjectHash($source, $properties);
122
		$prop = $this->getContext()->getProperty();
123
		if(isset($collections[$hash]) && count($collections[$hash]) > 0)
124
		{
125
			if(count($collections[$hash]) > 1)
126
				throw new TActiveRecordException('ar_belongs_to_multiple_result');
127
			$source->setColumnValue($prop, $collections[$hash][0]);
128
		}
129
	}
130
 
131
	/**
132
	 * Updates the associated foreign objects.
133
	 * @return boolean true if all update are success (including if no update was required), false otherwise .
134
	 */
135
	public function updateAssociatedRecords()
136
	{
137
		$fkObject = $this->getContext()->getPropertyValue();
138
		$source = $this->getSourceRecord();
139
		$fkeys = $this->findForeignKeys($fkObject, $source);
140
		foreach($fkeys as $fKey => $srcKey)
141
			$fkObject->setColumnValue($fKey, $source->getColumnValue($srcKey));
142
		return $fkObject->save();
143
	}
144
}
145