| [ Index ] |
PHP Cross Reference of Limb3 |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 * Limb PHP Framework 4 * 5 * @link http://limb-project.com 6 * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com) 7 * @license LGPL http://www.gnu.org/copyleft/lesser.html 8 */ 9 lmb_require('limb/core/src/lmbCollectionInterface.interface.php'); 10 lmb_require('limb/core/src/lmbCollection.class.php'); 11 lmb_require('limb/dbal/src/criteria/lmbSQLCriteria.class.php'); 12 13 /** 14 * abstract class lmbARRelationCollection. 15 * 16 * @package active_record 17 * @version $Id: lmbARRelationCollection.class.php 5997 2007-06-18 12:27:21Z pachanga $ 18 */ 19 abstract class lmbARRelationCollection implements lmbCollectionInterface 20 { 21 protected $relation; 22 protected $relation_info; 23 protected $owner; 24 protected $dataset; 25 protected $criteria; 26 protected $conn; 27 protected $is_owner_new; 28 protected $decorators = array(); 29 30 function __construct($relation, $owner, $criteria = null, $conn = null) 31 { 32 $this->relation = $relation; 33 $this->owner = $owner; 34 $this->relation_info = $owner->getRelationInfo($relation); 35 $this->criteria = lmbSQLCriteria :: objectify($criteria); 36 37 if(is_object($conn)) 38 $this->conn = $conn; 39 else 40 $this->conn = lmbToolkit :: instance()->getDefaultDbConnection(); 41 42 $this->reset(); 43 } 44 45 function setCriteria($criteria) 46 { 47 $this->criteria = lmbSQLCriteria :: objectify($criteria); 48 } 49 50 function reset() 51 { 52 $this->is_owner_new = $this->owner->isNew(); 53 $this->dataset = null; 54 } 55 56 protected function _ensureDataset() 57 { 58 if(is_object($this->dataset)) 59 return; 60 61 if($this->is_owner_new) 62 $this->dataset = new lmbCollection(); 63 else 64 $this->dataset = $this->find(); 65 } 66 67 abstract protected function _createDbRecordSet($criteria = null); 68 69 function find($magic_params = array()) 70 { 71 if($this->is_owner_new) 72 throw new lmbException('Not implemented for in memory collection'); 73 74 return $this->_createDecoratedDbRecordSet($magic_params); 75 } 76 77 function findFirst($magic_params = array()) 78 { 79 $rs = $this->find($magic_params); 80 $rs->rewind(); 81 if($rs->valid()) 82 return $rs->current(); 83 } 84 85 protected function _createDecoratedDbRecordSet($magic_params = array()) 86 { 87 $class = $this->relation_info['class']; 88 $object = new $class(); 89 90 $criteria = clone $this->criteria; 91 92 $sort_params = array(); 93 $has_class_criteria = false; 94 95 if(is_string($magic_params) || is_object($magic_params)) 96 $criteria->addAnd($magic_params); 97 elseif(is_array($magic_params)) 98 { 99 if(isset($magic_params['criteria'])) 100 $criteria->addAnd($magic_params['criteria']); 101 102 if(isset($magic_params['class'])) 103 { 104 $filter_object = new $magic_params['class']; 105 $criteria = $filter_object->addClassCriteria($criteria); 106 $has_class_criteria = true; 107 } 108 109 if(isset($magic_params['sort'])) 110 $sort_params = $magic_params['sort']; 111 } 112 113 if(!$has_class_criteria) 114 $object->addClassCriteria($criteria); 115 116 $rs = $this->_createDbRecordSet($criteria); 117 $this->_applySortParams($rs, $sort_params); 118 $dataset = $object->_decorateRecordSet($rs); 119 return $this->_applyDecorators($dataset); 120 } 121 122 protected function _applySortParams($rs, $sort_params = array()) 123 { 124 if(count($sort_params)) 125 { 126 $rs->sort($sort_params); 127 return; 128 } 129 130 if(isset($this->relation_info['sort_params']) && 131 is_array($this->relation_info['sort_params']) && 132 count($this->relation_info['sort_params'])) 133 { 134 $rs->sort($this->relation_info['sort_params']); 135 return; 136 } 137 138 $class = $this->relation_info['class']; 139 $object = new $class(); 140 if(count($default_sort_params = $object->getDefaultSortParams())) 141 { 142 $rs->sort($default_sort_params); 143 return; 144 } 145 } 146 147 function rewind() 148 { 149 $this->_ensureDataset(); 150 151 return $this->dataset->rewind(); 152 } 153 154 function next() 155 { 156 return $this->dataset->next(); 157 } 158 159 function current() 160 { 161 return $this->dataset->current(); 162 } 163 164 function valid() 165 { 166 return $this->dataset->valid(); 167 } 168 169 function key() 170 { 171 return $this->dataset->key(); 172 } 173 174 function add($object) 175 { 176 if(!$this->is_owner_new) 177 { 178 $this->_saveObject($object); 179 $this->dataset = null; 180 } 181 else 182 { 183 $this->_ensureDataset(); 184 $this->dataset->add($object); 185 } 186 } 187 188 function save($error_list = null) 189 { 190 $this->_ensureDataset(); 191 192 if(is_a($this->dataset, 'lmbCollection')) 193 { 194 foreach($this->dataset as $object) 195 $this->_saveObject($object, $error_list); 196 } 197 198 $this->reset(); 199 } 200 201 function getArray() 202 { 203 $result = array(); 204 foreach($this as $record) 205 $result[] = $record; 206 return $result; 207 } 208 209 function getIds() 210 { 211 $result = array(); 212 foreach($this->getArray() as $record) 213 $result[] = $record->getId(); 214 return $result; 215 } 216 217 //ArrayAccess interface 218 function offsetExists($offset) 219 { 220 return !is_null($this->offsetGet($offset)); 221 } 222 223 function offsetGet($offset) 224 { 225 if(is_numeric($offset)) 226 return $this->at((int)$offset); 227 } 228 229 function offsetSet($offset, $value) 230 { 231 if(!isset($offset)) 232 $this->add($value); 233 } 234 235 function offsetUnset($offset){} 236 //end 237 238 //Countable interface 239 function count() 240 { 241 $this->_ensureDataset(); 242 return $this->dataset->count(); 243 } 244 //end 245 246 function at($pos) 247 { 248 $this->_ensureDataset(); 249 return $this->dataset->at($pos); 250 } 251 252 function paginate($offset, $limit) 253 { 254 $this->_ensureDataset(); 255 $this->dataset->paginate($offset, $limit); 256 return $this; 257 } 258 259 function getLimit() 260 { 261 return $this->dataset->getLimit(); 262 } 263 264 function getOffset() 265 { 266 return $this->dataset->getOffset(); 267 } 268 269 function sort($params) 270 { 271 $this->_ensureDataset(); 272 $this->dataset->sort($params); 273 return $this; 274 } 275 276 function countPaginated() 277 { 278 return $this->dataset->countPaginated(); 279 } 280 281 function removeAll() 282 { 283 if($this->is_owner_new) 284 return $this->reset(); 285 286 $this->_removeRelatedRecords(); 287 } 288 289 abstract function set($objects); 290 291 abstract protected function _removeRelatedRecords(); 292 293 abstract protected function _saveObject($object, $error_list = null); 294 295 function addDecorator($decorator, $params = array()) 296 { 297 $this->decorators[] = array($decorator, $params); 298 } 299 300 protected function _applyDecorators($dataset) 301 { 302 $toolkit = lmbToolkit :: instance(); 303 304 foreach($this->decorators as $decorator_data) 305 { 306 $class_path = new lmbClassPath($decorator_data[0]); 307 $dataset = $class_path->createObject(array($dataset)); 308 $this->_addParamsToDataset($dataset, $decorator_data[1]); 309 } 310 return $dataset; 311 } 312 313 protected function _addParamsToDataset($dataset, $params) 314 { 315 foreach($params as $param => $value) 316 { 317 $method = lmb_camel_case('set_'.$param, false); 318 $dataset->$method($value); 319 } 320 } 321 } 322 323 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Sep 8 04:35:41 2008 | Cross-referenced by PHPXref 0.7 |