| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
@copyright |
|---|
| 6 |
@author will@flourishlib.com |
|---|
| 7 |
@license http://flourishlib.com/license |
|---|
| 8 |
|
|---|
| 9 |
@package |
|---|
| 10 |
@link http://flourishlib.com/fRecordSet |
|---|
| 11 |
|
|---|
| 12 |
@version |
|---|
| 13 |
@changes |
|---|
| 14 |
@changes |
|---|
| 15 |
@changes |
|---|
| 16 |
@changes |
|---|
| 17 |
@changes |
|---|
| 18 |
@changes |
|---|
| 19 |
@changes http://php.net/count |
|---|
| 20 |
@changes |
|---|
| 21 |
@changes |
|---|
| 22 |
@changes |
|---|
| 23 |
@changes |
|---|
| 24 |
@changes |
|---|
| 25 |
@changes |
|---|
| 26 |
@changes |
|---|
| 27 |
@changes |
|---|
| 28 |
@changes |
|---|
| 29 |
@changes |
|---|
| 30 |
@changes |
|---|
| 31 |
@changes |
|---|
| 32 |
@changes |
|---|
| 33 |
@changes |
|---|
| 34 |
@changes |
|---|
| 35 |
@changes |
|---|
| 36 |
@changes |
|---|
| 37 |
@changes |
|---|
| 38 |
@changes |
|---|
| 39 |
|
|---|
| 40 |
class fRecordSet implements Iterator, Countable |
|---|
| 41 |
{ |
|---|
| 42 |
|
|---|
| 43 |
const build = 'fRecordSet::build'; |
|---|
| 44 |
const buildFromArray = 'fRecordSet::buildFromArray'; |
|---|
| 45 |
const buildFromSQL = 'fRecordSet::buildFromSQL'; |
|---|
| 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 |
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
@param |
|---|
| 155 |
@param |
|---|
| 156 |
@param |
|---|
| 157 |
@param |
|---|
| 158 |
@param |
|---|
| 159 |
@return |
|---|
| 160 |
|
|---|
| 161 |
static public function build($class, $where_conditions=array(), $order_bys=array(), $limit=NULL, $page=NULL) |
|---|
| 162 |
{ |
|---|
| 163 |
self::validateClass($class); |
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
fActiveRecord::forceConfigure($class); |
|---|
| 167 |
|
|---|
| 168 |
$table = fORM::tablize($class); |
|---|
| 169 |
|
|---|
| 170 |
$sql = "SELECT " . $table . ".* FROM :from_clause"; |
|---|
| 171 |
|
|---|
| 172 |
if ($where_conditions) { |
|---|
| 173 |
$having_conditions = fORMDatabase::splitHavingConditions($where_conditions); |
|---|
| 174 |
|
|---|
| 175 |
$sql .= ' WHERE ' . fORMDatabase::createWhereClause($table, $where_conditions); |
|---|
| 176 |
} |
|---|
| 177 |
|
|---|
| 178 |
$sql .= ' :group_by_clause '; |
|---|
| 179 |
|
|---|
| 180 |
if ($where_conditions && $having_conditions) { |
|---|
| 181 |
$sql .= ' HAVING ' . fORMDatabase::createHavingClause($having_conditions); |
|---|
| 182 |
} |
|---|
| 183 |
|
|---|
| 184 |
if ($order_bys) { |
|---|
| 185 |
$sql .= ' ORDER BY ' . fORMDatabase::createOrderByClause($table, $order_bys); |
|---|
| 186 |
|
|---|
| 187 |
|
|---|
| 188 |
} elseif ($primary_keys = fORMSchema::retrieve()->getKeys($table, 'primary')) { |
|---|
| 189 |
$expressions = array(); |
|---|
| 190 |
foreach ($primary_keys as $primary_key) { |
|---|
| 191 |
$expressions[] = $table . '.' . $primary_key . ' ASC'; |
|---|
| 192 |
} |
|---|
| 193 |
$sql .= ' ORDER BY ' . join(', ', $expressions); |
|---|
| 194 |
} |
|---|
| 195 |
|
|---|
| 196 |
$sql = fORMDatabase::insertFromAndGroupByClauses($table, $sql); |
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
$non_limited_count_sql = NULL; |
|---|
| 200 |
if ($limit !== NULL) { |
|---|
| 201 |
$primary_key_fields = fORMSchema::retrieve()->getKeys($table, 'primary'); |
|---|
| 202 |
$primary_key_fields = fORMDatabase::addTableToValues($table, $primary_key_fields); |
|---|
| 203 |
|
|---|
| 204 |
$non_limited_count_sql = str_replace('SELECT ' . $table . '.*', 'SELECT ' . join(', ', $primary_key_fields), $sql); |
|---|
| 205 |
$non_limited_count_sql = 'SELECT count(*) FROM (' . $non_limited_count_sql . ') AS sq'; |
|---|
| 206 |
|
|---|
| 207 |
$sql .= ' LIMIT ' . $limit; |
|---|
| 208 |
|
|---|
| 209 |
if ($page !== NULL) { |
|---|
| 210 |
|
|---|
| 211 |
if (!is_numeric($page) || $page < 1) { |
|---|
| 212 |
throw new fProgrammerException( |
|---|
| 213 |
'The page specified, %s, is not a number or less than one', |
|---|
| 214 |
$page |
|---|
| 215 |
); |
|---|
| 216 |
} |
|---|
| 217 |
|
|---|
| 218 |
$sql .= ' OFFSET ' . (($page-1) * $limit); |
|---|
| 219 |
} |
|---|
| 220 |
} |
|---|
| 221 |
|
|---|
| 222 |
return new fRecordSet($class, fORMDatabase::retrieve()->translatedQuery($sql), $non_limited_count_sql); |
|---|
| 223 |
} |
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 |
@internal |
|---|
| 230 |
|
|---|
| 231 |
@param |
|---|
| 232 |
@param |
|---|
| 233 |
@return |
|---|
| 234 |
|
|---|
| 235 |
static public function buildFromArray($class, $records) |
|---|
| 236 |
{ |
|---|
| 237 |
if (is_array($class)) { |
|---|
| 238 |
foreach ($class as $_class) { |
|---|
| 239 |
self::validateClass($_class); |
|---|
| 240 |
} |
|---|
| 241 |
} else { |
|---|
| 242 |
self::validateClass($class); |
|---|
| 243 |
} |
|---|
| 244 |
|
|---|
| 245 |
if (!is_array($records)) { |
|---|
| 246 |
throw new fProgrammerException('The records specified are not in an array'); |
|---|
| 247 |
} |
|---|
| 248 |
|
|---|
| 249 |
return new fRecordSet($class, $records); |
|---|
| 250 |
} |
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 |
|
|---|
| 257 |
|
|---|
| 258 |
|
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 |
|
|---|
| 264 |
|
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 |
|
|---|
| 270 |
|
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 |
|
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 |
|
|---|
| 278 |
|
|---|
| 279 |
|
|---|
| 280 |
|
|---|
| 281 |
|
|---|
| 282 |
|
|---|
| 283 |
|
|---|
| 284 |
|
|---|
| 285 |
|
|---|
| 286 |
|
|---|
| 287 |
|
|---|
| 288 |
|
|---|
| 289 |
|
|---|
| 290 |
@param |
|---|
| 291 |
@param |
|---|
| 292 |
@param |
|---|
| 293 |
@return |
|---|
| 294 |
|
|---|
| 295 |
static public function buildFromSQL($class, $sql, $non_limited_count_sql=NULL) |
|---|
| 296 |
{ |
|---|
| 297 |
self::validateClass($class); |
|---|
| 298 |
|
|---|
| 299 |
if (!preg_match('#^\s*SELECT\s*(DISTINCT|ALL)?\s*(\w+\.)?\*\s*FROM#i', $sql)) { |
|---|
| 300 |
throw new fProgrammerException( |
|---|
| 301 |
'The SQL statement specified, %s, does not appear to be in the form SELECT * FROM table', |
|---|
| 302 |
$sql |
|---|
| 303 |
); |
|---|
| 304 |
} |
|---|
| 305 |
|
|---|
| 306 |
return new fRecordSet( |
|---|
| 307 |
$class, |
|---|
| 308 |
fORMDatabase::retrieve()->translatedQuery($sql), |
|---|
| 309 |
$non_limited_count_sql |
|---|
| 310 |
); |
|---|
| 311 |
} |
|---|
| 312 |
|
|---|
| 313 |
|
|---|
| 314 |
|
|---|
| 315 |
|
|---|
| 316 |
|
|---|
| 317 |
@param |
|---|
| 318 |
@param |
|---|
| 319 |
@param |
|---|
| 320 |
@return |
|---|
| 321 |
|
|---|
| 322 |
static protected function compose($message) |
|---|
| 323 |
{ |
|---|
| 324 |
$args = array_slice(func_get_args(), 1); |
|---|
| 325 |
|
|---|
| 326 |
if (class_exists('fText', FALSE)) { |
|---|
| 327 |
return call_user_func_array( |
|---|
| 328 |
array('fText', 'compose'), |
|---|
| 329 |
array($message, $args) |
|---|
| 330 |
); |
|---|
| 331 |
} else { |
|---|
| 332 |
return vsprintf($message, $args); |
|---|
| 333 |
} |
|---|
| 334 |
} |
|---|
| 335 |
|
|---|
| 336 |
|
|---|
| 337 |
|
|---|
| 338 |
|
|---|
| 339 |
|
|---|
| 340 |
@param |
|---|
| 341 |
@return |
|---|
| 342 |
|
|---|
| 343 |
static private function validateClass($class) |
|---|
| 344 |
{ |
|---|
| 345 |
$is_active_record = $class == 'fActiveRecord' || is_subclass_of($class, 'fActiveRecord'); |
|---|
| 346 |
if (!is_string($class) || !$class || !class_exists($class) || !$is_active_record) { |
|---|
| 347 |
throw new fProgrammerException( |
|---|
| 348 |
'The class specified, %1$s, does not appear to be a valid %2$s class', |
|---|
| 349 |
$class, |
|---|
| 350 |
'fActiveRecord' |
|---|
| 351 |
); |
|---|
| 352 |
} |
|---|
| 353 |
} |
|---|
| 354 |
|
|---|
| 355 |
|
|---|
| 356 |
|
|---|
| 357 |
|
|---|
| 358 |
|
|---|
| 359 |
@var |
|---|
| 360 |
|
|---|
| 361 |
private $class = NULL; |
|---|
| 362 |
|
|---|
| 363 |
|
|---|
| 364 |
|
|---|
| 365 |
|
|---|
| 366 |
@var |
|---|
| 367 |
|
|---|
| 368 |
private $non_limited_count = NULL; |
|---|
| 369 |
|
|---|
| 370 |
|
|---|
| 371 |
|
|---|
| 372 |
|
|---|
| 373 |
@var |
|---|
| 374 |
|
|---|
| 375 |
private $pointer = 0; |
|---|
| 376 |
|
|---|
| 377 |
|
|---|
| 378 |
|
|---|
| 379 |
|
|---|
| 380 |
@var |
|---|
| 381 |
|
|---|
| 382 |
private $records = array(); |
|---|
| 383 |
|
|---|
| 384 |
|
|---|
| 385 |
|
|---|
| 386 |
|
|---|
| 387 |
|
|---|
| 388 |
|
|---|
| 389 |
|
|---|
| 390 |
|
|---|
| 391 |
|
|---|
| 392 |
|
|---|
| 393 |
|
|---|
| 394 |
|
|---|
| 395 |
|
|---|
| 396 |
|
|---|
| 397 |
|
|---|
| 398 |
|
|---|
| 399 |
|
|---|
| 400 |
@param |
|---|
| 401 |
@param |
|---|
| 402 |
@return |
|---|
| 403 |
|
|---|
| 404 |
public function __call($method_name, $parameters) |
|---|
| 405 |
{ |
|---|
| 406 |
if ($callback = fORM::getRecordSetMethod($method_name)) { |
|---|
| 407 |
return call_user_func_array( |
|---|
| 408 |
$callback, |
|---|
| 409 |
array( |
|---|
| 410 |
$this, |
|---|
| 411 |
$this->class, |
|---|
| 412 |
&$this->records, |
|---|
| 413 |
&$this->pointer, |
|---|
| 414 |
$parameters |
|---|
| 415 |
) |
|---|
| 416 |
); |
|---|
| 417 |
} |
|---|
| 418 |
|
|---|
| 419 |
list($action, $element) = fORM::parseMethod($method_name); |
|---|
| 420 |
|
|---|
| 421 |
$route = ($parameters) ? $parameters[0] : NULL; |
|---|
| 422 |
|
|---|
| 423 |
|
|---|
| 424 |
if (in_array($action, array('build', 'prebuild', 'precount', 'precreate'))) { |
|---|
| 425 |
$related_class = fGrammar::singularize(fGrammar::camelize($element, TRUE)); |
|---|
| 426 |
} |
|---|
| 427 |
|
|---|
| 428 |
switch ($action) { |
|---|
| 429 |
case 'build': |
|---|
| 430 |
if ($route) { |
|---|
| 431 |
$this->precreate($related_class, $route); |
|---|
| 432 |
$this->buildFromCall('create' . $related_class, $route); |
|---|
| 433 |
} |
|---|
| 434 |
$this->precreate($related_class); |
|---|
| 435 |
return $this->buildFromCall('create' . $related_class); |
|---|
| 436 |
|
|---|
| 437 |
case 'prebuild': |
|---|
| 438 |
return $this->prebuild($related_class, $route); |
|---|
| 439 |
|
|---|
| 440 |
case 'precount': |
|---|
| 441 |
return $this->precount($related_class, $route); |
|---|
| 442 |
|
|---|
| 443 |
case 'precreate': |
|---|
| 444 |
return $this->precreate($related_class, $route); |
|---|
| 445 |
} |
|---|
| 446 |
|
|---|
| 447 |
throw new fProgrammerException( |
|---|
| 448 |
'Unknown method, %s(), called', |
|---|
| 449 |
$method_name |
|---|
| 450 |
); |
|---|
| 451 |
} |
|---|
| 452 |
|
|---|
| 453 |
|
|---|
| 454 |
|
|---|
| 455 |
|
|---|
| 456 |
|
|---|
| 457 |
@param |
|---|
| 458 |
@param |
|---|
| 459 |
@param |
|---|
| 460 |
@return |
|---|
| 461 |
|
|---|
| 462 |
protected function __construct($class, $records=NULL, $non_limited_count=NULL) |
|---|
| 463 |
{ |
|---|
| 464 |
$this->class = (is_array($class) && count($class) == 1) ? current($class) : $class; |
|---|
| 465 |
|
|---|
| 466 |
if ($non_limited_count !== NULL) { |
|---|
| 467 |
$this->non_limited_count = $non_limited_count; |
|---|
| 468 |
} |
|---|
| 469 |
|
|---|
| 470 |
if ($records && is_object($records) && $records instanceof Iterator) { |
|---|
| 471 |
while ($records->valid()) { |
|---|
| 472 |
$this->records[] = new $class($records); |
|---|
| 473 |
$records->next(); |
|---|
| 474 |
} |
|---|
| 475 |
} |
|---|
| 476 |
|
|---|
| 477 |
if (is_array($records)) { |
|---|
| 478 |
$this->records = $records; |
|---|
| 479 |
} |
|---|
| 480 |
} |
|---|
| 481 |
|
|---|
| 482 |
|
|---|
| 483 |
|
|---|
| 484 |
|
|---|
| 485 |
|
|---|
| 486 |
@internal |
|---|
| 487 |
|
|---|
| 488 |
@param |
|---|
| 489 |
@return |
|---|
| 490 |
|
|---|
| 491 |
public function __get($method) |
|---|
| 492 |
{ |
|---|
| 493 |
return array($this, $method); |
|---|
| 494 |
} |
|---|
| 495 |
|
|---|
| 496 |
|
|---|
| 497 |
|
|---|
| 498 |
|
|---|
| 499 |
|
|---|
| 500 |
@param |
|---|
| 501 |
@param |
|---|
| 502 |
@param |
|---|
| 503 |
@return |
|---|
| 504 |
|
|---|
| 505 |
public function buildFromCall($method) |
|---|
| 506 |
{ |
|---|
| 507 |
$parameters = func_get_args(); |
|---|
| 508 |
|
|---|
| 509 |
$result = call_user_func_array($this->call, $parameters); |
|---|
| 510 |
|
|---|
| 511 |
$classes = array(); |
|---|
| 512 |
foreach ($result as $record) { |
|---|
| 513 |
if (!$record instanceof fActiveRecord) { |
|---|
| 514 |
throw new fProgrammerException( |
|---|
| 515 |
'The method called, %1$s, returned something other than an fActiveRecord object', |
|---|
| 516 |
$method |
|---|
| 517 |
); |
|---|
| 518 |
} |
|---|
| 519 |
|
|---|
| 520 |
$class = get_class($record); |
|---|
| 521 |
|
|---|
| 522 |
if (!isset($classes[$class])) { |
|---|
| 523 |
$classes[$class] = TRUE; |
|---|
| 524 |
} |
|---|
| 525 |
} |
|---|
| 526 |
|
|---|
| 527 |
|
|---|
| 528 |
if (!$classes) { |
|---|
| 529 |
$classes = array('fActiveRecord' => TRUE); |
|---|
| 530 |
} |
|---|
| 531 |
|
|---|
| 532 |
return new fRecordSet(array_keys($classes), $result); |
|---|
| 533 |
} |
|---|
| 534 |
|
|---|
| 535 |
|
|---|
| 536 |
|
|---|
| 537 |
|
|---|
| 538 |
|
|---|
| 539 |
@param |
|---|
| 540 |
@param |
|---|
| 541 |
@param |
|---|
| 542 |
@return |
|---|
| 543 |
|
|---|
| 544 |
public function buildFromMap($callback) |
|---|
| 545 |
{ |
|---|
| 546 |
$parameters = func_get_args(); |
|---|
| 547 |
|
|---|
| 548 |
$result = call_user_func_array($this->map, $parameters); |
|---|
| 549 |
|
|---|
| 550 |
$classes = array(); |
|---|
| 551 |
foreach ($result as $record) { |
|---|
| 552 |
if (!$record instanceof fActiveRecord) { |
|---|
| 553 |
throw new fProgrammerException( |
|---|
| 554 |
'The map operation specified, %1$s, returned something other than an fActiveRecord object', |
|---|
| 555 |
$callback |
|---|
| 556 |
); |
|---|
| 557 |
} |
|---|
| 558 |
|
|---|
| 559 |
$class = get_class($record); |
|---|
| 560 |
|
|---|
| 561 |
if (!isset($classes[$class])) { |
|---|
| 562 |
$classes[$class] = TRUE; |
|---|
| 563 |
} |
|---|
| 564 |
} |
|---|
| 565 |
|
|---|
| 566 |
|
|---|
| 567 |
if (!$classes) { |
|---|
| 568 |
$classes = array('fActiveRecord' => TRUE); |
|---|
| 569 |
} |
|---|
| 570 |
|
|---|
| 571 |
return new fRecordSet(array_keys($classes), $result); |
|---|
| 572 |
} |
|---|
| 573 |
|
|---|
| 574 |
|
|---|
| 575 |
|
|---|
| 576 |
|
|---|
| 577 |
|
|---|
| 578 |
@param |
|---|
| 579 |
@param |
|---|
| 580 |
@param |
|---|
| 581 |
@return |
|---|
| 582 |
|
|---|
| 583 |
public function call($method) |
|---|
| 584 |
{ |
|---|
| 585 |
$parameters = array_slice(func_get_args(), 1); |
|---|
| 586 |
|
|---|
| 587 |
$output = array(); |
|---|
| 588 |
foreach ($this->records as $record) { |
|---|
| 589 |
$output[] = call_user_func_array( |
|---|
| 590 |
$record->$method, |
|---|
| 591 |
$parameters |
|---|
| 592 |
); |
|---|
| 593 |
} |
|---|
| 594 |
return $output; |
|---|
| 595 |
} |
|---|
| 596 |
|
|---|
| 597 |
|
|---|
| 598 |
|
|---|
| 599 |
|
|---|
| 600 |
|
|---|
| 601 |
@param |
|---|
| 602 |
@return |
|---|
| 603 |
|
|---|
| 604 |
private function constructOrderByClause($route=NULL) |
|---|
| 605 |
{ |
|---|
| 606 |
$table = fORM::tablize($this->class); |
|---|
| 607 |
$table_with_route = ($route) ? $table . '{' . $route . '}' : $table; |
|---|
| 608 |
|
|---|
| 609 |
$pk_columns = fORMSchema::retrieve()->getKeys($table, 'primary'); |
|---|
| 610 |
$first_pk_column = $pk_columns[0]; |
|---|
| 611 |
|
|---|
| 612 |
$sql = ''; |
|---|
| 613 |
|
|---|
| 614 |
$number = 0; |
|---|
| 615 |
foreach ($this->getPrimaryKeys() as $primary_key) { |
|---|
| 616 |
$sql .= 'WHEN '; |
|---|
| 617 |
|
|---|
| 618 |
if (is_array($primary_key)) { |
|---|
| 619 |
$conditions = array(); |
|---|
| 620 |
foreach ($pk_columns as $pk_column) { |
|---|
| 621 |
$conditions[] = $table_with_route . '.' . $pk_column . fORMDatabase::escapeBySchema($table, $pk_column, $primary_key[$pk_column], '='); |
|---|
| 622 |
} |
|---|
| 623 |
$sql .= join(' AND ', $conditions); |
|---|
| 624 |
} else { |
|---|
| 625 |
$sql .= $table_with_route . '.' . $first_pk_column . fORMDatabase::escapeBySchema($table, $first_pk_column, $primary_key, '='); |
|---|
| 626 |
} |
|---|
| 627 |
|
|---|
| 628 |
$sql .= ' THEN ' . $number . ' '; |
|---|
| 629 |
|
|---|
| 630 |
$number++; |
|---|
| 631 |
} |
|---|
| 632 |
|
|---|
| 633 |
return 'CASE ' . $sql . 'END ASC'; |
|---|
| 634 |
} |
|---|
| 635 |
|
|---|
| 636 |
|
|---|
| 637 |
|
|---|
| 638 |
|
|---|
| 639 |
|
|---|
| 640 |
@param |
|---|
| 641 |
@return |
|---|
| 642 |
|
|---|
| 643 |
private function constructWhereClause($route=NULL) |
|---|
| 644 |
{ |
|---|
| 645 |
$table = fORM::tablize($this->class); |
|---|
| 646 |
$table_with_route = ($route) ? $table . '{' . $route . '}' : $table; |
|---|
| 647 |
|
|---|
| 648 |
$pk_columns = fORMSchema::retrieve()->getKeys($table, 'primary'); |
|---|
| 649 |
|
|---|
| 650 |
$sql = ''; |
|---|
| 651 |
|
|---|
| 652 |
|
|---|
| 653 |
if (sizeof($pk_columns) > 1) { |
|---|
| 654 |
|
|---|
| 655 |
$conditions = array(); |
|---|
| 656 |
|
|---|
| 657 |
foreach ($this->getPrimaryKeys() as $primary_key) { |
|---|
| 658 |
$sub_conditions = array(); |
|---|
| 659 |
foreach ($pk_columns as $pk_column) { |
|---|
| 660 |
$sub_conditions[] = $table_with_route . '.' . $pk_column . fORMDatabase::escapeBySchema($table, $pk_column, $primary_key[$pk_column], '='); |
|---|
| 661 |
} |
|---|
| 662 |
$conditions[] = join(' AND ', $sub_conditions); |
|---|
| 663 |
} |
|---|
| 664 |
$sql .= '(' . join(') OR (', $conditions) . ')'; |
|---|
| 665 |
|
|---|
| 666 |
|
|---|
| 667 |
} else { |
|---|
| 668 |
$first_pk_column = $pk_columns[0]; |
|---|
| 669 |
|
|---|
| 670 |
$values = array(); |
|---|
| 671 |
foreach ($this->getPrimaryKeys() as $primary_key) { |
|---|
| 672 |
$values[] = fORMDatabase::escapeBySchema($table, $first_pk_column, $primary_key); |
|---|
| 673 |
} |
|---|
| 674 |
$sql .= $table_with_route . '.' . $first_pk_column . ' IN (' . join(', ', $values) . ')'; |
|---|
| 675 |
} |
|---|
| 676 |
|
|---|
| 677 |
return $sql; |
|---|
| 678 |
} |
|---|
| 679 |
|
|---|
| 680 |
|
|---|
| 681 |
|
|---|
| 682 |
|
|---|
| 683 |
|
|---|
| 684 |
@param |
|---|
| 685 |
@return |
|---|
| 686 |
|
|---|
| 687 |
public function contains($record) |
|---|
| 688 |
{ |
|---|
| 689 |
$class = get_class($record); |
|---|
| 690 |
if (!in_array($class, (array) $this->class)) { |
|---|
| 691 |
return FALSE; |
|---|
| 692 |
} |
|---|
| 693 |
|
|---|
| 694 |
if (!$record->exists()) { |
|---|
| 695 |
throw new fProgrammerException( |
|---|
| 696 |
'Only records that exist can be checked for in the record set' |
|---|
| 697 |
); |
|---|
| 698 |
} |
|---|
| 699 |
|
|---|
| 700 |
$hash = fActiveRecord::hash($record); |
|---|
| 701 |
|
|---|
| 702 |
foreach ($this->records as $_record) { |
|---|
| 703 |
if ($class != get_class($_record)) { |
|---|
| 704 |
continue; |
|---|
| 705 |
} |
|---|
| 706 |
if ($hash == fActiveRecord::hash($_record)) { |
|---|
| 707 |
return TRUE; |
|---|
| 708 |
} |
|---|
| 709 |
} |
|---|
| 710 |
|
|---|
| 711 |
return FALSE; |
|---|
| 712 |
} |
|---|
| 713 |
|
|---|
| 714 |
|
|---|
| 715 |
|
|---|
| 716 |
|
|---|
| 717 |
|
|---|
| 718 |
@param |
|---|
| 719 |
@return |
|---|
| 720 |
|
|---|
| 721 |
public function count($ignore_limit=FALSE) |
|---|
| 722 |
{ |
|---|
| 723 |
if ($ignore_limit !== TRUE || $this->non_limited_count === NULL) { |
|---|
| 724 |
return sizeof($this->records); |
|---|
| 725 |
} |
|---|
| 726 |
|
|---|
| 727 |
if (!is_numeric($this->non_limited_count)) { |
|---|
| 728 |
try { |
|---|
| 729 |
$this->non_limited_count = fORMDatabase::retrieve()->translatedQuery($this->non_limited_count)->fetchScalar(); |
|---|
| 730 |
} catch (fExpectedException $e) { |
|---|
| 731 |
$this->non_limited_count = $this->count(); |
|---|
| 732 |
} |
|---|
| 733 |
} |
|---|
| 734 |
return $this->non_limited_count; |
|---|
| 735 |
} |
|---|
| 736 |
|
|---|
| 737 |
|
|---|
| 738 |
|
|---|
| 739 |
|
|---|
| 740 |
|
|---|
| 741 |
@throws |
|---|
| 742 |
@internal |
|---|
| 743 |
|
|---|
| 744 |
@return |
|---|
| 745 |
|
|---|
| 746 |
public function current() |
|---|
| 747 |
{ |
|---|
| 748 |
if (!$this->valid()) { |
|---|
| 749 |
throw new fNoRemainingException( |
|---|
| 750 |
'There are no remaining records' |
|---|
| 751 |
); |
|---|
| 752 |
} |
|---|
| 753 |
|
|---|
| 754 |
return $this->records[$this->pointer]; |
|---|
| 755 |
} |
|---|
| 756 |
|
|---|
| 757 |
|
|---|
| 758 |
|
|---|
| 759 |
|
|---|
| 760 |
|
|---|
| 761 |
@param |
|---|
| 762 |
@param |
|---|
| 763 |
@return |
|---|
| 764 |
|
|---|
| 765 |
public function diff($records, $remember_original_count=FALSE) |
|---|
| 766 |
{ |
|---|
| 767 |
$remove_records = array(); |
|---|
| 768 |
|
|---|
| 769 |
if ($records instanceof fActiveRecord) { |
|---|
| 770 |
$records = array($records); |
|---|
| 771 |
} |
|---|
| 772 |
foreach ($records as $record) { |
|---|
| 773 |
$class = get_class($record); |
|---|
| 774 |
$hash = fActiveRecord::hash($record); |
|---|
| 775 |
$remove_records[$class . '::' . $hash] = TRUE; |
|---|
| 776 |
} |
|---|
| 777 |
|
|---|
| 778 |
$new_records = array(); |
|---|
| 779 |
$classes = array(); |
|---|
| 780 |
|
|---|
| 781 |
foreach ($this->records as $record) { |
|---|
| 782 |
$class = get_class($record); |
|---|
| 783 |
$hash = fActiveRecord::hash($record); |
|---|
| 784 |
if (!isset($remove_records[$class . '::' . $hash])) { |
|---|
| 785 |
$new_records[] = $record; |
|---|
| 786 |
$classes[$class] = TRUE; |
|---|
| 787 |
} |
|---|
| 788 |
} |
|---|
| 789 |
|
|---|
| 790 |
if ($classes) { |
|---|
| 791 |
$class = array_keys($classes); |
|---|
| 792 |
} else { |
|---|
| 793 |
$class = $this->class; |
|---|
| 794 |
} |
|---|
| 795 |
|
|---|
| 796 |
$set = new fRecordSet($class, $new_records); |
|---|
| 797 |
|
|---|
| 798 |
if ($remember_original_count) { |
|---|
| 799 |
$set->non_limited_count = $this->count(); |
|---|
| 800 |
} |
|---|
| 801 |
|
|---|
| 802 |
return $set; |
|---|
| 803 |
} |
|---|
| 804 |
|
|---|
| 805 |
|
|---|
| 806 |
|
|---|
| 807 |
|
|---|
| 808 |
|
|---|
| 809 |
|
|---|
| 810 |
|
|---|
| 811 |
|
|---|
| 812 |
|
|---|
| 813 |
|
|---|
| 814 |
|
|---|
| 815 |
|
|---|
| 816 |
|
|---|
| 817 |
|
|---|
| 818 |
|
|---|
| 819 |
|
|---|
| 820 |
|
|---|
| 821 |
|
|---|
| 822 |
|
|---|
| 823 |
|
|---|
| 824 |
|
|---|
| 825 |
|
|---|
| 826 |
|
|---|
| 827 |
|
|---|
| 828 |
|
|---|
| 829 |
|
|---|
| 830 |
|
|---|
| 831 |
|
|---|
| 832 |
|
|---|
| 833 |
|
|---|
| 834 |
|
|---|
| 835 |
|
|---|
| 836 |
|
|---|
| 837 |
|
|---|
| 838 |
|
|---|
| 839 |
|
|---|
| 840 |
|
|---|
| 841 |
|
|---|
| 842 |
|
|---|
| 843 |
|
|---|
| 844 |
|
|---|
| 845 |
|
|---|
| 846 |
|
|---|
| 847 |
|
|---|
| 848 |
|
|---|
| 849 |
|
|---|
| 850 |
|
|---|
| 851 |
@param |
|---|
| 852 |
@param |
|---|
| 853 |
@return |
|---|
| 854 |
|
|---|
| 855 |
public function filter($procedure, $remember_original_count=FALSE) |
|---|
| 856 |
{ |
|---|
| 857 |
if (!$this->records) { |
|---|
| 858 |
return clone $this; |
|---|
| 859 |
} |
|---|
| 860 |
|
|---|
| 861 |
if (is_array($procedure) && is_string(key($procedure))) { |
|---|
| 862 |
$type = 'conditions'; |
|---|
| 863 |
$conditions = $procedure; |
|---|
| 864 |
|
|---|
| 865 |
} elseif (is_string($procedure) && preg_match('#^\{record\}::([a-z0-9_\-]+)$#iD', $procedure, $matches)) { |
|---|
| 866 |
$type = 'psuedo-callback'; |
|---|
| 867 |
$method = $matches[1]; |
|---|
| 868 |
|
|---|
| 869 |
} else { |
|---|
| 870 |
$type = 'callback'; |
|---|
| 871 |
$callback = $procedure; |
|---|
| 872 |
if (is_string($callback) && strpos($callback, '::') !== FALSE) { |
|---|
| 873 |
$callback = explode('::', $callback); |
|---|
| 874 |
} |
|---|
| 875 |
} |
|---|
| 876 |
|
|---|
| 877 |
$new_records = array(); |
|---|
| 878 |
$classes = (!is_array($this->class)) ? array($this->class => TRUE) : array(); |
|---|
| 879 |
|
|---|
| 880 |
foreach ($this->records as $record) { |
|---|
| 881 |
switch ($type) { |
|---|
| 882 |
case 'conditions': |
|---|
| 883 |
$value = fActiveRecord::checkConditions($record, $conditions); |
|---|
| 884 |
break; |
|---|
| 885 |
|
|---|
| 886 |
case 'psuedo-callback': |
|---|
| 887 |
$value = $record->$method(); |
|---|
| 888 |
break; |
|---|
| 889 |
|
|---|
| 890 |
case 'callback': |
|---|
| 891 |
$value = call_user_func($callback, $record); |
|---|
| 892 |
break; |
|---|
| 893 |
} |
|---|
| 894 |
|
|---|
| 895 |
if ($value) { |
|---|
| 896 |
$classes[get_class($record)] = TRUE; |
|---|
| 897 |
|
|---|
| 898 |
$new_records[] = $record; |
|---|
| 899 |
} |
|---|
| 900 |
} |
|---|
| 901 |
|
|---|
| 902 |
$set = new fRecordSet(array_keys($classes), $new_records); |
|---|
| 903 |
|
|---|
| 904 |
if ($remember_original_count) { |
|---|
| 905 |
$set->non_limited_count = $this->count(); |
|---|
| 906 |
} |
|---|
| 907 |
|
|---|
| 908 |
return $set; |
|---|
| 909 |
} |
|---|
| 910 |
|
|---|
| 911 |
|
|---|
| 912 |
|
|---|
| 913 |
|
|---|
| 914 |
|
|---|
| 915 |
@throws |
|---|
| 916 |
|
|---|
| 917 |
@return |
|---|
| 918 |
|
|---|
| 919 |
public function fetchRecord() |
|---|
| 920 |
{ |
|---|
| 921 |
$record = $this->current(); |
|---|
| 922 |
$this->next(); |
|---|
| 923 |
return $record; |
|---|
| 924 |
} |
|---|
| 925 |
|
|---|
| 926 |
|
|---|
| 927 |
|
|---|
| 928 |
|
|---|
| 929 |
|
|---|
| 930 |
@return |
|---|
| 931 |
|
|---|
| 932 |
public function getClass() |
|---|
| 933 |
{ |
|---|
| 934 |
return $this->class; |
|---|
| 935 |
} |
|---|
| 936 |
|
|---|
| 937 |
|
|---|
| 938 |
|
|---|
| 939 |
|
|---|
| 940 |
|
|---|
| 941 |
@return |
|---|
| 942 |
|
|---|
| 943 |
public function getRecords() |
|---|
| 944 |
{ |
|---|
| 945 |
return $this->records; |
|---|
| 946 |
} |
|---|
| 947 |
|
|---|
| 948 |
|
|---|
| 949 |
|
|---|
| 950 |
|
|---|
| 951 |
|
|---|
| 952 |
@return |
|---|
| 953 |
|
|---|
| 954 |
public function getPrimaryKeys() |
|---|
| 955 |
{ |
|---|
| 956 |
if (!sizeof($this->records)) { |
|---|
| 957 |
return array(); |
|---|
| 958 |
} |
|---|
| 959 |
|
|---|
| 960 |
$this->validateSingleClass('get primary key'); |
|---|
| 961 |
|
|---|
| 962 |
$table = fORM::tablize($this->class); |
|---|
| 963 |
$pk_columns = fORMSchema::retrieve()->getKeys($table, 'primary'); |
|---|
| 964 |
$first_pk_column = $pk_columns[0]; |
|---|
| 965 |
|
|---|
| 966 |
$primary_keys = array(); |
|---|
| 967 |
|
|---|
| 968 |
foreach ($this->records as $number => $record) { |
|---|
| 969 |
$keys = array(); |
|---|
| 970 |
|
|---|
| 971 |
foreach ($pk_columns as $pk_column) { |
|---|
| 972 |
$method = 'get' . fGrammar::camelize($pk_column, TRUE); |
|---|
| 973 |
$keys[$pk_column] = $record->$method(); |
|---|
| 974 |
} |
|---|
| 975 |
|
|---|
| 976 |
$primary_keys[$number] = (sizeof($pk_columns) == 1) ? $keys[$first_pk_column] : $keys; |
|---|
| 977 |
} |
|---|
| 978 |
|
|---|
| 979 |
return $primary_keys; |
|---|
| 980 |
} |
|---|
| 981 |
|
|---|
| 982 |
|
|---|
| 983 |
|
|---|
| 984 |
|
|---|
| 985 |
|
|---|
| 986 |
@param |
|---|
| 987 |
@param |
|---|
| 988 |
@return |
|---|
| 989 |
|
|---|
| 990 |
public function intersect($records, $remember_original_count=FALSE) |
|---|
| 991 |
{ |
|---|
| 992 |
$hashes = array(); |
|---|
| 993 |
|
|---|
| 994 |
if ($records instanceof fActiveRecord) { |
|---|
| 995 |
$records = array($records); |
|---|
| 996 |
} |
|---|
| 997 |
foreach ($records as $record) { |
|---|
| 998 |
$class = get_class($record); |
|---|
| 999 |
$hash = fActiveRecord::hash($record); |
|---|
| 1000 |
$hashes[$class . '::' . $hash] = TRUE; |
|---|
| 1001 |
} |
|---|
| 1002 |
|
|---|
| 1003 |
$new_records = array(); |
|---|
| 1004 |
$classes = array(); |
|---|
| 1005 |
|
|---|
| 1006 |
foreach ($this->records as $record) { |
|---|
| 1007 |
$class = get_class($record); |
|---|
| 1008 |
$hash = fActiveRecord::hash($record); |
|---|
| 1009 |
if (isset($hashes[$class . '::' . $hash])) { |
|---|
| 1010 |
$new_records[] = $record; |
|---|
| 1011 |
$classes[$class] = TRUE; |
|---|
| 1012 |
} |
|---|
| 1013 |
} |
|---|
| 1014 |
|
|---|
| 1015 |
if ($classes) { |
|---|
| 1016 |
$class = array_keys($classes); |
|---|
| 1017 |
} else { |
|---|
| 1018 |
$class = $this->class; |
|---|
| 1019 |
} |
|---|
| 1020 |
|
|---|
| 1021 |
$set = new fRecordSet($class, $new_records); |
|---|
| 1022 |
|
|---|
| 1023 |
if ($remember_original_count) { |
|---|
| 1024 |
$set->non_limited_count = $this->count(); |
|---|
| 1025 |
} |
|---|
| 1026 |
|
|---|
| 1027 |
return $set; |
|---|
| 1028 |
} |
|---|
| 1029 |
|
|---|
| 1030 |
|
|---|
| 1031 |
|
|---|
| 1032 |
|
|---|
| 1033 |
|
|---|
| 1034 |
@internal |
|---|
| 1035 |
|
|---|
| 1036 |
@return |
|---|
| 1037 |
|
|---|
| 1038 |
public function key() |
|---|
| 1039 |
{ |
|---|
| 1040 |
return $this->pointer; |
|---|
| 1041 |
} |
|---|
| 1042 |
|
|---|
| 1043 |
|
|---|
| 1044 |
|
|---|
| 1045 |
http://php.net/array_map |
|---|
| 1046 |
|
|---|
| 1047 |
|
|---|
| 1048 |
|
|---|
| 1049 |
|
|---|
| 1050 |
|
|---|
| 1051 |
|
|---|
| 1052 |
|
|---|
| 1053 |
|
|---|
| 1054 |
|
|---|
| 1055 |
|
|---|
| 1056 |
|
|---|
| 1057 |
|
|---|
| 1058 |
|
|---|
| 1059 |
|
|---|
| 1060 |
|
|---|
| 1061 |
|
|---|
| 1062 |
|
|---|
| 1063 |
|
|---|
| 1064 |
|
|---|
| 1065 |
|
|---|
| 1066 |
|
|---|
| 1067 |
|
|---|
| 1068 |
@param |
|---|
| 1069 |
@param |
|---|
| 1070 |
@param |
|---|
| 1071 |
@return |
|---|
| 1072 |
|
|---|
| 1073 |
public function map($callback) |
|---|
| 1074 |
{ |
|---|
| 1075 |
$parameters = array_slice(func_get_args(), 1); |
|---|
| 1076 |
|
|---|
| 1077 |
if (!$this->records) { |
|---|
| 1078 |
return array(); |
|---|
| 1079 |
} |
|---|
| 1080 |
|
|---|
| 1081 |
$parameters_array = array(); |
|---|
| 1082 |
$found_record = FALSE; |
|---|
| 1083 |
$total_records = sizeof($this->records); |
|---|
| 1084 |
|
|---|
| 1085 |
foreach ($parameters as $parameter) { |
|---|
| 1086 |
if (!is_array($parameter)) { |
|---|
| 1087 |
if (preg_match('#^\{record\}::([a-z0-9_\-]+)$#iD', $parameter, $matches)) { |
|---|
| 1088 |
$parameters_array[] = $this->call($matches[1]); |
|---|
| 1089 |
$found_record = TRUE; |
|---|
| 1090 |
} elseif ($parameter === '{record}') { |
|---|
| 1091 |
$parameters_array[] = $this->records; |
|---|
| 1092 |
$found_record = TRUE; |
|---|
| 1093 |
} elseif ($parameter === '{index}') { |
|---|
| 1094 |
$parameters_array[] = array_keys($this->records); |
|---|
| 1095 |
} else { |
|---|
| 1096 |
$parameters_array[] = array_pad(array(), $total_records, $parameter); |
|---|
| 1097 |
} |
|---|
| 1098 |
|
|---|
| 1099 |
} elseif (sizeof($parameter) > $total_records) { |
|---|
| 1100 |
$parameters_array[] = array_slice($parameter, 0, $total_records); |
|---|
| 1101 |
} elseif (sizeof($parameter) < $total_records) { |
|---|
| 1102 |
$parameters_array[] = array_pad($parameter, $total_records, NULL); |
|---|
| 1103 |
} else { |
|---|
| 1104 |
$parameters_array[] = $parameter; |
|---|
| 1105 |
} |
|---|
| 1106 |
} |
|---|
| 1107 |
|
|---|
| 1108 |
if (!$found_record) { |
|---|
| 1109 |
array_unshift($parameters_array, $this->records); |
|---|
| 1110 |
} |
|---|
| 1111 |
|
|---|
| 1112 |
if (is_string($callback) && strpos($callback, '::') !== FALSE) { |
|---|
| 1113 |
$callback = explode('::', $callback); |
|---|
| 1114 |
} |
|---|
| 1115 |
|
|---|
| 1116 |
array_unshift($parameters_array, $callback); |
|---|
| 1117 |
|
|---|
| 1118 |
return call_user_func_array('array_map', $parameters_array); |
|---|
| 1119 |
} |
|---|
| 1120 |
|
|---|
| 1121 |
|
|---|
| 1122 |
|
|---|
| 1123 |
|
|---|
| 1124 |
|
|---|
| 1125 |
@param |
|---|
| 1126 |
@return |
|---|
| 1127 |
|
|---|
| 1128 |
public function merge($records) |
|---|
| 1129 |
{ |
|---|
| 1130 |
$classes = array_flip((array) $this->class); |
|---|
| 1131 |
|
|---|
| 1132 |
if ($records instanceof fRecordSet) { |
|---|
| 1133 |
$new_records = $records->records; |
|---|
| 1134 |
$classes += array_flip((array) $records->class); |
|---|
| 1135 |
|
|---|
| 1136 |
} elseif (is_array($records)) { |
|---|
| 1137 |
$new_records = array(); |
|---|
| 1138 |
foreach ($records as $record) { |
|---|
| 1139 |
if (!$record instanceof fActiveRecord) { |
|---|
| 1140 |
throw new fProgrammerException( |
|---|
| 1141 |
'One of the records specified is not an instance of %s', |
|---|
| 1142 |
'fActiveRecord' |
|---|
| 1143 |
); |
|---|
| 1144 |
} |
|---|
| 1145 |
$new_records[] = $record; |
|---|
| 1146 |
$classes[get_class($record)] = TRUE; |
|---|
| 1147 |
} |
|---|
| 1148 |
|
|---|
| 1149 |
} elseif ($records instanceof fActiveRecord) { |
|---|
| 1150 |
$new_records = array($records); |
|---|
| 1151 |
$classes[get_class($records)] = TRUE; |
|---|
| 1152 |
|
|---|
| 1153 |
} else { |
|---|
| 1154 |
throw new fProgrammerException( |
|---|
| 1155 |
'The records specified, %1$s, are invalid. Must be an %2$s, %3$s or an array of %4$s.', |
|---|
| 1156 |
$records, |
|---|
| 1157 |
'fRecordSet', |
|---|
| 1158 |
'fActiveRecord', |
|---|
| 1159 |
'fActiveRecords' |
|---|
| 1160 |
); |
|---|
| 1161 |
} |
|---|
| 1162 |
|
|---|
| 1163 |
if (!$new_records) { |
|---|
| 1164 |
return $this; |
|---|
| 1165 |
} |
|---|
| 1166 |
|
|---|
| 1167 |
return new fRecordSet( |
|---|
| 1168 |
array_keys($classes), |
|---|
| 1169 |
array_merge( |
|---|
| 1170 |
$this->records, |
|---|
| 1171 |
$new_records |
|---|
| 1172 |
) |
|---|
| 1173 |
); |
|---|
| 1174 |
} |
|---|
| 1175 |
|
|---|
| 1176 |
|
|---|
| 1177 |
|
|---|
| 1178 |
|
|---|
| 1179 |
|
|---|
| 1180 |
@internal |
|---|
| 1181 |
|
|---|
| 1182 |
@return |
|---|
| 1183 |
|
|---|
| 1184 |
public function next() |
|---|
| 1185 |
{ |
|---|
| 1186 |
$this->pointer++; |
|---|
| 1187 |
} |
|---|
| 1188 |
|
|---|
| 1189 |
|
|---|
| 1190 |
|
|---|
| 1191 |
|
|---|
| 1192 |
|
|---|
| 1193 |
@param |
|---|
| 1194 |
@param |
|---|
| 1195 |
@return |
|---|
| 1196 |
|
|---|
| 1197 |
private function prebuild($related_class, $route=NULL) |
|---|
| 1198 |
{ |
|---|
| 1199 |
if (!$this->records) { |
|---|
| 1200 |
return $this; |
|---|
| 1201 |
} |
|---|
| 1202 |
|
|---|
| 1203 |
$this->validateSingleClass('prebuild'); |
|---|
| 1204 |
|
|---|
| 1205 |
|
|---|
| 1206 |
if (!array_merge($this->getPrimaryKeys())) { |
|---|
| 1207 |
return $this; |
|---|
| 1208 |
} |
|---|
| 1209 |
|
|---|
| 1210 |
$related_table = fORM::tablize($related_class); |
|---|
| 1211 |
$table = fORM::tablize($this->class); |
|---|
| 1212 |
|
|---|
| 1213 |
$route = fORMSchema::getRouteName($table, $related_table, $route, '*-to-many'); |
|---|
| 1214 |
$relationship = fORMSchema::getRoute($table, $related_table, $route, '*-to-many'); |
|---|
| 1215 |
|
|---|
| 1216 |
$table_with_route = ($route) ? $table . '{' . $route . '}' : $table; |
|---|
| 1217 |
|
|---|
| 1218 |
|
|---|
| 1219 |
$where_sql = $this->constructWhereClause($route); |
|---|
| 1220 |
|
|---|
| 1221 |
$order_by_sql = $this->constructOrderByClause($route); |
|---|
| 1222 |
if ($related_order_bys = fORMRelated::getOrderBys($this->class, $related_class, $route)) { |
|---|
| 1223 |
$order_by_sql .= ', ' . fORMDatabase::createOrderByClause($related_table, $related_order_bys); |
|---|
| 1224 |
} |
|---|
| 1225 |
|
|---|
| 1226 |
$new_sql = 'SELECT ' . $related_table . '.*'; |
|---|
| 1227 |
|
|---|
| 1228 |
|
|---|
| 1229 |
if (isset($relationship['join_table'])) { |
|---|
| 1230 |
$new_sql .= ", " . $table_with_route . '.' . $relationship['column']; |
|---|
| 1231 |
} |
|---|
| 1232 |
|
|---|
| 1233 |
$new_sql .= ' FROM :from_clause '; |
|---|
| 1234 |
$new_sql .= ' WHERE ' . $where_sql; |
|---|
| 1235 |
$new_sql .= ' :group_by_clause '; |
|---|
| 1236 |
$new_sql .= ' ORDER BY ' . $order_by_sql; |
|---|
| 1237 |
|
|---|
| 1238 |
$new_sql = fORMDatabase::insertFromAndGroupByClauses($related_table, $new_sql); |
|---|
| 1239 |
|
|---|
| 1240 |
|
|---|
| 1241 |
if (strpos($new_sql, 'GROUP BY') !== FALSE) { |
|---|
| 1242 |
$new_sql = str_replace(' ORDER BY', ', ' . $table . '.' . $relationship['column'] . ' ORDER BY', $new_sql); |
|---|
| 1243 |
} |
|---|
| 1244 |
|
|---|
| 1245 |
|
|---|
| 1246 |
|
|---|
| 1247 |
$result = fORMDatabase::retrieve()->translatedQuery($new_sql); |
|---|
| 1248 |
|
|---|
| 1249 |
$total_records = sizeof($this->records); |
|---|
| 1250 |
for ($i=0; $i < $total_records; $i++) { |
|---|
| 1251 |
|
|---|
| 1252 |
|
|---|
| 1253 |
|
|---|
| 1254 |
$record = $this->records[$i]; |
|---|
| 1255 |
$keys = array(); |
|---|
| 1256 |
|
|---|
| 1257 |
|
|---|
| 1258 |
|
|---|
| 1259 |
if (isset($relationship['join_table'])) { |
|---|
| 1260 |
try { |
|---|
| 1261 |
$current_row = $result->current(); |
|---|
| 1262 |
$keys[$relationship['column']] = $current_row[$relationship['column']]; |
|---|
| 1263 |
} catch (fExpectedException $e) { } |
|---|
| 1264 |
|
|---|
| 1265 |
|
|---|
| 1266 |
} else { |
|---|
| 1267 |
$method = 'get' . fGrammar::camelize($relationship['related_column'], TRUE); |
|---|
| 1268 |
$keys[$relationship['related_column']] = $record->$method(); |
|---|
| 1269 |
} |
|---|
| 1270 |
|
|---|
| 1271 |
|
|---|
| 1272 |
|
|---|
| 1273 |
$rows = array(); |
|---|
| 1274 |
|
|---|
| 1275 |
try { |
|---|
| 1276 |
while (!array_diff_assoc($keys, $result->current())) { |
|---|
| 1277 |
$row = $result->fetchRow(); |
|---|
| 1278 |
|
|---|
| 1279 |
|
|---|
| 1280 |
if (isset($relationship['join_table'])) { |
|---|
| 1281 |
unset($row[$relationship['column']]); |
|---|
| 1282 |
} |
|---|
| 1283 |
|
|---|
| 1284 |
$rows[] = $row; |
|---|
| 1285 |
} |
|---|
| 1286 |
} catch (fExpectedException $e) { } |
|---|
| 1287 |
|
|---|
| 1288 |
|
|---|
| 1289 |
|
|---|
| 1290 |
$set = new fRecordSet($related_class, new ArrayIterator($rows)); |
|---|
| 1291 |
|
|---|
| 1292 |
|
|---|
| 1293 |
$method = 'inject' . fGrammar::pluralize($related_class); |
|---|
| 1294 |
$record->$method($set, $route); |
|---|
| 1295 |
} |
|---|
| 1296 |
|
|---|
| 1297 |
return $this; |
|---|
| 1298 |
} |
|---|
| 1299 |
|
|---|
| 1300 |
|
|---|
| 1301 |
|
|---|
| 1302 |
|
|---|
| 1303 |
|
|---|
| 1304 |
@param |
|---|
| 1305 |
@param |
|---|
| 1306 |
@return |
|---|
| 1307 |
|
|---|
| 1308 |
private function precount($related_class, $route=NULL) |
|---|
| 1309 |
{ |
|---|
| 1310 |
if (!$this->records) { |
|---|
| 1311 |
return $this; |
|---|
| 1312 |
} |
|---|
| 1313 |
|
|---|
| 1314 |
$this->validateSingleClass('precount'); |
|---|
| 1315 |
|
|---|
| 1316 |
|
|---|
| 1317 |
if (!array_merge($this->getPrimaryKeys())) { |
|---|
| 1318 |
return $this; |
|---|
| 1319 |
} |
|---|
| 1320 |
|
|---|
| 1321 |
$related_table = fORM::tablize($related_class); |
|---|
| 1322 |
$table = fORM::tablize($this->class); |
|---|
| 1323 |
|
|---|
| 1324 |
$route = fORMSchema::getRouteName($table, $related_table, $route, '*-to-many'); |
|---|
| 1325 |
$relationship = fORMSchema::getRoute($table, $related_table, $route, '*-to-many'); |
|---|
| 1326 |
|
|---|
| 1327 |
$table_with_route = ($route) ? $table . '{' . $route . '}' : $table; |
|---|
| 1328 |
|
|---|
| 1329 |
|
|---|
| 1330 |
$where_sql = $this->constructWhereClause($route); |
|---|
| 1331 |
$order_by_sql = $this->constructOrderByClause($route); |
|---|
| 1332 |
|
|---|
| 1333 |
$related_table_keys = fORMSchema::retrieve()->getKeys($related_table, 'primary'); |
|---|
| 1334 |
$related_table_keys = fORMDatabase::addTableToValues($related_table, $related_table_keys); |
|---|
| 1335 |
$related_table_keys = join(', ', $related_table_keys); |
|---|
| 1336 |
|
|---|
| 1337 |
$column = $table_with_route . '.' . $relationship['column']; |
|---|
| 1338 |
|
|---|
| 1339 |
$new_sql = 'SELECT count(' . $related_table_keys . ') AS __flourish_count, ' . $column . ' AS __flourish_column '; |
|---|
| 1340 |
$new_sql .= ' FROM :from_clause '; |
|---|
| 1341 |
$new_sql .= ' WHERE ' . $where_sql; |
|---|
| 1342 |
$new_sql .= ' GROUP BY ' . $column; |
|---|
| 1343 |
$new_sql .= ' ORDER BY ' . $column . ' ASC'; |
|---|
| 1344 |
|
|---|
| 1345 |
$new_sql = fORMDatabase::insertFromAndGroupByClauses($related_table, $new_sql); |
|---|
| 1346 |
|
|---|
| 1347 |
|
|---|
| 1348 |
$result = fORMDatabase::retrieve()->translatedQuery($new_sql); |
|---|
| 1349 |
|
|---|
| 1350 |
$counts = array(); |
|---|
| 1351 |
foreach ($result as $row) { |
|---|
| 1352 |
$counts[$row['__flourish_column']] = (int) $row['__flourish_count']; |
|---|
| 1353 |
} |
|---|
| 1354 |
|
|---|
| 1355 |
unset($result); |
|---|
| 1356 |
|
|---|
| 1357 |
$total_records = sizeof($this->records); |
|---|
| 1358 |
$get_method = 'get' . fGrammar::camelize($relationship['column'], TRUE); |
|---|
| 1359 |
$tally_method = 'tally' . fGrammar::pluralize($related_class); |
|---|
| 1360 |
|
|---|
| 1361 |
for ($i=0; $i < $total_records; $i++) { |
|---|
| 1362 |
$record = $this->records[$i]; |
|---|
| 1363 |
$count = (isset($counts[$record->$get_method()])) ? $counts[$record->$get_method()] : 0; |
|---|
| 1364 |
$record->$tally_method($count, $route); |
|---|
| 1365 |
} |
|---|
| 1366 |
|
|---|
| 1367 |
return $this; |
|---|
| 1368 |
} |
|---|
| 1369 |
|
|---|
| 1370 |
|
|---|
| 1371 |
|
|---|
| 1372 |
|
|---|
| 1373 |
|
|---|
| 1374 |
@param |
|---|
| 1375 |
@param |
|---|
| 1376 |
@return |
|---|
| 1377 |
|
|---|
| 1378 |
private function precreate($related_class, $route=NULL) |
|---|
| 1379 |
{ |
|---|
| 1380 |
if (!$this->records) { |
|---|
| 1381 |
return $this; |
|---|
| 1382 |
} |
|---|
| 1383 |
|
|---|
| 1384 |
$this->validateSingleClass('precreate'); |
|---|
| 1385 |
|
|---|
| 1386 |
|
|---|
| 1387 |
if (!array_merge($this->getPrimaryKeys())) { |
|---|
| 1388 |
return $this; |
|---|
| 1389 |
} |
|---|
| 1390 |
|
|---|
| 1391 |
$relationship = fORMSchema::getRoute( |
|---|
| 1392 |
fORM::tablize($this->class), |
|---|
| 1393 |
fORM::tablize($related_class), |
|---|
| 1394 |
$route, |
|---|
| 1395 |
'*-to-one' |
|---|
| 1396 |
); |
|---|
| 1397 |
|
|---|
| 1398 |
$values = $this->call('get' . fGrammar::camelize($relationship['column'], TRUE)); |
|---|
| 1399 |
$values = array_unique($values); |
|---|
| 1400 |
|
|---|
| 1401 |
self::build( |
|---|
| 1402 |
$related_class, |
|---|
| 1403 |
array( |
|---|
| 1404 |
$relationship['related_column'] . '=' => $values |
|---|
| 1405 |
) |
|---|
| 1406 |
); |
|---|
| 1407 |
|
|---|
| 1408 |
return $this; |
|---|
| 1409 |
} |
|---|
| 1410 |
|
|---|
| 1411 |
|
|---|
| 1412 |
|
|---|
| 1413 |
|
|---|
| 1414 |
|
|---|
| 1415 |
|
|---|
| 1416 |
|
|---|
| 1417 |
|
|---|
| 1418 |
|
|---|
| 1419 |
|
|---|
| 1420 |
@param |
|---|
| 1421 |
@param |
|---|
| 1422 |
@return |
|---|
| 1423 |
|
|---|
| 1424 |
public function reduce($callback, $inital_value=NULL) |
|---|
| 1425 |
{ |
|---|
| 1426 |
if (!$this->records) { |
|---|
| 1427 |
return $initial_value; |
|---|
| 1428 |
} |
|---|
| 1429 |
|
|---|
| 1430 |
$result = $inital_value; |
|---|
| 1431 |
|
|---|
| 1432 |
if (is_string($callback) && strpos($callback, '::') !== FALSE) { |
|---|
| 1433 |
$callback = explode('::', $callback); |
|---|
| 1434 |
} |
|---|
| 1435 |
|
|---|
| 1436 |
foreach($this->records as $record) { |
|---|
| 1437 |
$result = call_user_func($callback, $result, $record); |
|---|
| 1438 |
} |
|---|
| 1439 |
|
|---|
| 1440 |
return $result; |
|---|
| 1441 |
} |
|---|
| 1442 |
|
|---|
| 1443 |
|
|---|
| 1444 |
|
|---|
| 1445 |
|
|---|
| 1446 |
|
|---|
| 1447 |
@internal |
|---|
| 1448 |
|
|---|
| 1449 |
@return |
|---|
| 1450 |
|
|---|
| 1451 |
public function rewind() |
|---|
| 1452 |
{ |
|---|
| 1453 |
$this->pointer = 0; |
|---|
| 1454 |
} |
|---|
| 1455 |
|
|---|
| 1456 |
|
|---|
| 1457 |
|
|---|
| 1458 |
|
|---|
| 1459 |
|
|---|
| 1460 |
@param |
|---|
| 1461 |
@param |
|---|
| 1462 |
@param |
|---|
| 1463 |
@return |
|---|
| 1464 |
|
|---|
| 1465 |
public function slice($offset, $length=NULL, $remember_original_count=FALSE) |
|---|
| 1466 |
{ |
|---|
| 1467 |
if ($length === NULL) { |
|---|
| 1468 |
if ($offset >= 0) { |
|---|
| 1469 |
$length = sizeof($this->records) - $offset; |
|---|
| 1470 |
} else { |
|---|
| 1471 |
$length = abs($offset); |
|---|
| 1472 |
} |
|---|
| 1473 |
} |
|---|
| 1474 |
|
|---|
| 1475 |
$set = new fRecordSet($this->class, array_slice($this->records, $offset, $length)); |
|---|
| 1476 |
|
|---|
| 1477 |
if ($remember_original_count) { |
|---|
| 1478 |
$set->non_limited_count = $this->count(); |
|---|
| 1479 |
} |
|---|
| 1480 |
|
|---|
| 1481 |
return $set; |
|---|
| 1482 |
} |
|---|
| 1483 |
|
|---|
| 1484 |
|
|---|
| 1485 |
|
|---|
| 1486 |
|
|---|
| 1487 |
|
|---|
| 1488 |
|
|---|
| 1489 |
|
|---|
| 1490 |
@param |
|---|
| 1491 |
@param |
|---|
| 1492 |
@return |
|---|
| 1493 |
|
|---|
| 1494 |
public function sort($method, $direction) |
|---|
| 1495 |
{ |
|---|
| 1496 |
if (!in_array($direction, array('asc', 'desc'))) { |
|---|
| 1497 |
throw new fProgrammerException( |
|---|
| 1498 |
'The sort direction specified, %1$s, is invalid. Must be one of: %2$s or %3$s.', |
|---|
| 1499 |
$direction, |
|---|
| 1500 |
'asc', |
|---|
| 1501 |
'desc' |
|---|
| 1502 |
); |
|---|
| 1503 |
} |
|---|
| 1504 |
|
|---|
| 1505 |
|
|---|
| 1506 |
$lambda_params = '$a,$b'; |
|---|
| 1507 |
$lambda_funcs = array( |
|---|
| 1508 |
'asc' => 'return fUTF8::inatcmp($a->' . $method . '(), $b->' . $method . '());', |
|---|
| 1509 |
'desc' => 'return fUTF8::inatcmp($b->' . $method . '(), $a->' . $method . '());' |
|---|
| 1510 |
); |
|---|
| 1511 |
|
|---|
| 1512 |
$this->sortByCallback(create_function($lambda_params, $lambda_funcs[$direction])); |
|---|
| 1513 |
|
|---|
| 1514 |
return $this; |
|---|
| 1515 |
} |
|---|
| 1516 |
|
|---|
| 1517 |
|
|---|
| 1518 |
|
|---|
| 1519 |
http://php.net/usort |
|---|
| 1520 |
|
|---|
| 1521 |
@param |
|---|
| 1522 |
@return |
|---|
| 1523 |
|
|---|
| 1524 |
public function sortByCallback($callback) |
|---|
| 1525 |
{ |
|---|
| 1526 |
usort($this->records, $callback); |
|---|
| 1527 |
$this->rewind(); |
|---|
| 1528 |
|
|---|
| 1529 |
return $this; |
|---|
| 1530 |
} |
|---|
| 1531 |
|
|---|
| 1532 |
|
|---|
| 1533 |
|
|---|
| 1534 |
|
|---|
| 1535 |
|
|---|
| 1536 |
@throws |
|---|
| 1537 |
|
|---|
| 1538 |
@param |
|---|
| 1539 |
@return |
|---|
| 1540 |
|
|---|
| 1541 |
public function tossIfEmpty($message=NULL) |
|---|
| 1542 |
{ |
|---|
| 1543 |
if ($this->records) { |
|---|
| 1544 |
return $this; |
|---|
| 1545 |
} |
|---|
| 1546 |
|
|---|
| 1547 |
if ($message === NULL) { |
|---|
| 1548 |
if (is_array($this->class)) { |
|---|
| 1549 |
$names = array_map(array('fORM', 'getRecordName'), $this->class); |
|---|
| 1550 |
$names = array_map(array('fGrammar', 'pluralize'), $names); |
|---|
| 1551 |
$name = join(', ', $names); |
|---|
| 1552 |
} else { |
|---|
| 1553 |
$name = fGrammar::pluralize(fORM::getRecordName($this->class)); |
|---|
| 1554 |
} |
|---|
| 1555 |
|
|---|
| 1556 |
$message = self::compose( |
|---|
| 1557 |
'No %s could be found', |
|---|
| 1558 |
$name |
|---|
| 1559 |
); |
|---|
| 1560 |
} |
|---|
| 1561 |
|
|---|
| 1562 |
throw new fEmptySetException($message); |
|---|
| 1563 |
} |
|---|
| 1564 |
|
|---|
| 1565 |
|
|---|
| 1566 |
|
|---|
| 1567 |
|
|---|
| 1568 |
|
|---|
| 1569 |
@param |
|---|
| 1570 |
@return |
|---|
| 1571 |
|
|---|
| 1572 |
public function unique($remember_original_count=FALSE) |
|---|
| 1573 |
{ |
|---|
| 1574 |
$records = array(); |
|---|
| 1575 |
|
|---|
| 1576 |
foreach ($this->records as $record) { |
|---|
| 1577 |
$class = get_class($record); |
|---|
| 1578 |
$hash = fActiveRecord::hash($record); |
|---|
| 1579 |
if (isset($records[$class . '::' . $hash])) { |
|---|
| 1580 |
continue; |
|---|
| 1581 |
} |
|---|
| 1582 |
$records[$class . '::' . $hash] = $record; |
|---|
| 1583 |
} |
|---|
| 1584 |
|
|---|
| 1585 |
$set = new fRecordSet( |
|---|
| 1586 |
$this->class, |
|---|
| 1587 |
array_values($records) |
|---|
| 1588 |
); |
|---|
| 1589 |
|
|---|
| 1590 |
if ($remember_original_count) { |
|---|
| 1591 |
$set->non_limited_count = $this->count(); |
|---|
| 1592 |
} |
|---|
| 1593 |
|
|---|
| 1594 |
return $set; |
|---|
| 1595 |
} |
|---|
| 1596 |
|
|---|
| 1597 |
|
|---|
| 1598 |
|
|---|
| 1599 |
|
|---|
| 1600 |
|
|---|
| 1601 |
@internal |
|---|
| 1602 |
|
|---|
| 1603 |
@return |
|---|
| 1604 |
|
|---|
| 1605 |
public function valid() |
|---|
| 1606 |
{ |
|---|
| 1607 |
return $this->pointer < $this->count(); |
|---|
| 1608 |
} |
|---|
| 1609 |
|
|---|
| 1610 |
|
|---|
| 1611 |
|
|---|
| 1612 |
|
|---|
| 1613 |
|
|---|
| 1614 |
@param |
|---|
| 1615 |
@return |
|---|
| 1616 |
|
|---|
| 1617 |
private function validateSingleClass($operation) |
|---|
| 1618 |
{ |
|---|
| 1619 |
if (!is_array($this->class) && $this->class != 'fActiveRecord') { |
|---|
| 1620 |
return; |
|---|
| 1621 |
} |
|---|
| 1622 |
|
|---|
| 1623 |
throw new fProgrammerException( |
|---|
| 1624 |
'The %1$s operation can not be performed on a record set with multiple types (%2$s) of records', |
|---|
| 1625 |
$operation, |
|---|
| 1626 |
join(', ', $this->class) |
|---|
| 1627 |
); |
|---|
| 1628 |
} |
|---|
| 1629 |
} |
|---|
| 1630 |
|
|---|
| 1631 |
|
|---|
| 1632 |
|
|---|
| 1633 |
|
|---|
| 1634 |
will@flourishlib.com |
|---|
| 1635 |
|
|---|
| 1636 |
|
|---|
| 1637 |
|
|---|
| 1638 |
|
|---|
| 1639 |
|
|---|
| 1640 |
|
|---|
| 1641 |
|
|---|
| 1642 |
|
|---|
| 1643 |
|
|---|
| 1644 |
|
|---|
| 1645 |
|
|---|
| 1646 |
|
|---|
| 1647 |
|
|---|
| 1648 |
|
|---|
| 1649 |
|
|---|
| 1650 |
|
|---|
| 1651 |
|
|---|
| 1652 |
|
|---|
| 1653 |
|
|---|