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