| [ Index ] |
PHP Cross Reference of Akelos Framework |
[Summary view] [Print] [Text view]
1 <?php 2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ 3 4 // +----------------------------------------------------------------------+ 5 // | Akelos Framework - http://www.akelos.org | 6 // +----------------------------------------------------------------------+ 7 // | Copyright (c) 2002-2006, Akelos Media, S.L. & Bermi Ferrer Martinez | 8 // | Released under the GNU Lesser General Public License, see LICENSE.txt| 9 // +----------------------------------------------------------------------+ 10 11 /** 12 * @package ActiveRecord 13 * @subpackage Associations 14 * @author Bermi Ferrer <bermi a.t akelos c.om> 15 * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. 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 var $Owner; 24 var $options = array(); 25 var $models = array(); 26 27 function AkAssociation(&$Owner) 28 { 29 $this->Owner =& $Owner; 30 $this->observe($this->Owner); 31 $this->_setAssociationAccesorAliasReferences(); 32 } 33 34 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 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 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 function setOptions($association_id, $options) 76 { 77 $this->options[$association_id] = $options; 78 } 79 80 function getOptions($association_id) 81 { 82 return $this->options[$association_id]; 83 } 84 85 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 function &addModel($association_id, &$associated_model) 91 { 92 $this->models[$association_id] =& $associated_model; 93 return $this->models[$association_id]; 94 95 } 96 97 function &getModel($association_id) 98 { 99 return $this->models[$association_id]; 100 } 101 102 function &getModels() 103 { 104 return $this->models; 105 } 106 107 function getAssociatedIds() 108 { 109 return array_keys($this->options); 110 } 111 112 113 function &_build($association_id, &$AssociatedObject, $reference_associated = true) 114 { 115 if($reference_associated){ 116 $this->Owner->$association_id =& $AssociatedObject; 117 }else{ 118 $this->Owner->$association_id = $AssociatedObject; 119 } 120 $this->Owner->$association_id->_AssociationHandler =& $this; 121 $this->Owner->$association_id->_associatedAs = $this->getType(); 122 $this->Owner->$association_id->_associationId = $association_id; 123 $this->Owner->_associations[$association_id] =& $this->Owner->$association_id; 124 return $this->Owner->$association_id; 125 } 126 127 function setAssociatedId($association_id, $associated_id) 128 { 129 $this->Owner->_associationIds[$association_id] = $associated_id; 130 $this->associated_ids[$association_id] = $associated_id; 131 } 132 133 function &loadAssociated($association_id) 134 { 135 if (!$this->Owner->isNewRecord()){ 136 if(empty($this->Owner->$association_id->_loaded)){ 137 if($Associated =& $this->findAssociated($association_id)){ 138 $Associated->_loaded = true; 139 $this->_build($association_id, $Associated, false); 140 } 141 } 142 } 143 144 return $this->Owner->$association_id; 145 } 146 147 /** 148 * Class interfaces. All Association objects must implement the following methods 149 */ 150 151 function addAssociated($association_id, $options = array()) 152 { 153 trigger_error(__FUNCTION__.' must be defined into your specific association handler'); 154 } 155 156 function getType() 157 { 158 trigger_error(__FUNCTION__.' must be defined into your specific association handler'); 159 } 160 161 function getAssociatedFinderSqlOptions() 162 { 163 trigger_error(__FUNCTION__.' must be defined into your specific association handler'); 164 } 165 166 function isOwnerAnActiveRecord() 167 { 168 return $this->__activeRecordObject; 169 } 170 171 172 function _hasTablePrefix($association_id) 173 { 174 return isset($this->$association_id->_associationTablePrefixes[$this->$association_id->_tableName]); 175 } 176 177 function _saveLoadedHandler($association_id, $associated) 178 { 179 $this->Owner->_association_handler_copies[$association_id] = $associated; 180 } 181 182 function _getLoadedHandler($association_id) 183 { 184 return $this->Owner->_association_handler_copies[$association_id]; 185 } 186 187 /** 188 * Recurses through $owner and its superclasses until it finds the class which defines the association to the given $associatedModel 189 */ 190 function _findOwnerTypeForAssociation(&$AssociatedModel, $Owner) { 191 if (!is_object($Owner)) { 192 $Owner = new $Owner; 193 } 194 $owner_type = AkInflector::underscore($Owner->getType()); 195 if (isset($AssociatedModel->$owner_type)) { 196 return $owner_type; 197 } else { 198 return $this->_findOwnerTypeForAssociation($AssociatedModel, get_parent_class($Owner)); 199 } 200 } 201 } 202 203 204 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Oct 27 12:43:49 2008 | Cross-referenced by PHPXref 0.6 |