root/trunk/lib/AkActiveRecord/AkAssociation.php

Revision 1399, 6.9 kB (checked in by bermi, 6 months ago)

When loading associations using load(), it will always return false is no associates are found.

Converting most Active record files to vanilla PHP5

Line 
1 <?php
2
3 // +----------------------------------------------------------------------+
4 // | Akelos Framework - http://www.akelos.org                             |
5 // +----------------------------------------------------------------------+
6 // | Released under the GNU Lesser General Public License, see LICENSE.txt|
7 // +----------------------------------------------------------------------+
8
9 /**
10  * @package ActiveRecord
11  * @subpackage Associations
12  * @author Bermi Ferrer <bermi a.t bermilabs c.om>
13  * @author Kaste
14  * @author Arno Schneider <arno a.t bermilabs c.om>
15  * @copyright Copyright (c) 2002-2009, The Akelos Team http://www.akelos.org
16  * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
17  */
18
19 require_once(AK_LIB_DIR.DS.'AkActiveRecord'.DS.'AkObserver.php');
20
21 class AkAssociation extends AkObserver
22 {
23     public $Owner;
24     public $options = array();
25     public $models = array();
26
27     public function AkAssociation(&$Owner)
28     {
29         $this->Owner =& $Owner;
30         $this->observe($this->Owner);
31         $this->_setAssociationAccesorAliasReferences();
32     }
33
34     public function initializeAssociated($options)
35     {
36         $options = is_string($options) ? array_map('trim',array_diff(explode(',',$options.','), array(''))) : $options;
37         if(is_array($options)){
38             foreach ($options as $k=>$option){
39                 if(is_numeric($k)){
40                     $association_id = $option;
41                     $association_options = array();
42                 }else{
43                     $association_id = $k;
44                     $association_options = $option;
45                 }
46                 if($association_id = $this->setAssociationId($association_id)){
47                     $this->addAssociated($association_id, $association_options);
48                 }
49             }
50         }
51     }
52
53     public function setAssociationId($association_id)
54     {
55         $association_id = strtolower(AkInflector::underscore($association_id));
56         if(isset($this->Owner->$association_id)){
57             trigger_error(Ak::t('Could not load %association_id on %model_name because "%model_name->%association_id" attribute '.
58             'is already defined and can\'t be used as an association placeholder',
59             array('%model_name'=>$this->Owner->getModelName(),'%association_id'=>$association_id)),
60             E_USER_ERROR);
61             return false;
62         }else {
63             return $association_id;
64         }
65     }
66
67     public function _setAssociationAccesorAliasReferences()
68     {
69         $underscored_alias = AkInflector::underscore($this->getType());
70         if(!isset($this->Owner->$underscored_alias)){
71             $this->Owner->$underscored_alias =& $this;
72         }
73     }
74
75     public function setOptions($association_id, $options)
76     {
77         $this->options[$association_id] = $options;
78     }
79
80     public function getOptions($association_id)
81     {
82         return $this->options[$association_id];
83     }
84
85     public function getOption($association_id, $option_name)
86     {
87         return isset($this->options[$association_id][$option_name]) ? $this->options[$association_id][$option_name] : false;
88     }
89
90     public function &addModel($association_id, &$associated_model)
91     {
92         $this->models[$association_id] =& $associated_model;
93         return $this->models[$association_id];
94
95     }
96
97     public function &getModel($association_id)
98     {
99         return $this->models[$association_id];
100     }
101
102     public function &getModels()
103     {
104         return $this->models;
105     }
106
107     public function getAssociatedIds()
108     {
109         return array_keys($this->options);
110     }
111     public function _getColumnParenthesis()
112     {
113         static $type;
114         if (empty($type)) {
115             $type=$this->Owner->_db->type();
116         }
117         return $type=='mysql'?"'":'"';
118     }
119
120     public function &_build($association_id, &$AssociatedObject, $reference_associated = true)
121     {
122         if($reference_associated){
123             $this->Owner->$association_id =& $AssociatedObject;
124         }else{
125             $this->Owner->$association_id = $AssociatedObject;
126         }
127         $this->Owner->$association_id->_AssociationHandler =& $this;
128         $this->Owner->$association_id->_associatedAs = $this->getType();
129         $this->Owner->$association_id->_associationId = $association_id;
130         $this->Owner->_associations[$association_id] =& $this->Owner->$association_id;
131         return $this->Owner->$association_id;
132     }
133
134     public function setAssociatedId($association_id, $associated_id)
135     {
136         $this->Owner->_associationIds[$association_id]  = $associated_id;
137         $this->associated_ids[$association_id] = $associated_id;
138     }
139
140     public function &loadAssociated($association_id, $return_false_if_not_found = false)
141     {
142         if (!$this->Owner->isNewRecord()){
143             if(empty($this->Owner->$association_id->_loaded)){
144                 if($Associated =& $this->findAssociated($association_id)){
145                     $Associated->_loaded = true;
146                     $this->_build($association_id, $Associated, false);
147                 }elseif ($return_false_if_not_found){
148                     $false = false;
149                     return $false;
150                 }
151             }
152         }
153         return $this->Owner->$association_id;
154     }
155
156     /**
157      * Class interfaces. All Association objects must implement the following methods
158      */
159
160     public function addAssociated($association_id, $options = array())
161     {
162         trigger_error(__FUNCTION__.' must be defined into your specific association handler');
163     }
164
165     public function getType()
166     {
167         trigger_error(__FUNCTION__.' must be defined into your specific association handler');
168     }
169
170     public function getAssociatedFinderSqlOptions()
171     {
172         trigger_error(__FUNCTION__.' must be defined into your specific association handler');
173     }
174
175     public function isOwnerAnActiveRecord()
176     {
177         return $this->__activeRecordObject;
178     }
179
180
181     public function _hasTablePrefix($association_id)
182     {
183         return isset($this->$association_id->_associationTablePrefixes[$this->$association_id->_tableName]);
184     }
185
186     public function _saveLoadedHandler($association_id, $associated)
187     {
188         $this->Owner->_association_handler_copies[$association_id] = $associated;
189     }
190
191     public function _getLoadedHandler($association_id)
192     {
193         return $this->Owner->_association_handler_copies[$association_id];
194     }
195
196     /**
197      * Recurses through $owner and its superclasses until it finds the class which defines the association to the given $associatedModel
198      */
199     public function _findOwnerTypeForAssociation(&$AssociatedModel, $Owner) {
200         if (!is_object($Owner)) {
201             $Owner = new $Owner;
202         }
203         $owner_type = AkInflector::underscore($Owner->getType());
204         if (isset($AssociatedModel->$owner_type)) {
205             return $owner_type;
206         } else {
207             return $this->_findOwnerTypeForAssociation($AssociatedModel, get_parent_class($Owner));
208         }
209     }
210 }
211
212
213 ?>
214
Note: See TracBrowser for help on using the browser.