Permalink
Browse files

Fixed tickets #225 and #270 - Loading fActiveRecord objects by multi-…

…column primary key now works even if the columns are in a different order, one-to-one relationships now are properly detected the ORM API was improved to handle them
  • Loading branch information...
wbond committed Jul 21, 2009
1 parent 12eee2d commit fbe71f0b84ccff97c3622fa831fdc11488a7776b
Showing with 360 additions and 92 deletions.
  1. +47 −14 classes/fActiveRecord.php
  2. +265 −72 classes/fORMRelated.php
  3. +39 −1 classes/fORMSchema.php
  4. +9 −5 classes/fSchema.php
View
@@ -15,7 +15,8 @@
* @package Flourish
* @link http://flourishlib.com/fActiveRecord
*
* @version 1.0.0b30
* @version 1.0.0b31
* @changes 1.0.0b31 Fixed a bug with loading a record by a multi-column primary key, fixed one-to-one relationship API [wb, 2009-07-21]
* @changes 1.0.0b30 Updated ::reflect() for new fORM::callReflectCallbacks() API [wb, 2009-07-13]
* @changes 1.0.0b29 Updated to use new fORM::callInspectCallbacks() method [wb, 2009-07-13]
* @changes 1.0.0b28 Fixed a bug where records would break the identity map at the end of ::store() [wb, 2009-07-09]
@@ -519,13 +520,33 @@ public function __call($method_name, $parameters)
// Related data methods
case 'associate':
$subject = fGrammar::singularize($subject);
$table = fORM::tablize($class);
$records = $parameters[0];
$route = isset($parameters[1]) ? $parameters[1] : NULL;
$plural = FALSE;
// one-to-many relationships need to use plural forms
if (in_array($subject, fORMSchema::retrieve()->getTables())) {
if (fORMSchema::isOneToOne($table, $subject, $route)) {
throw new fProgrammerException(
'The table %1$s is not in a %2$srelationship with the table %3$s',
$table,
'*-to-many ',
$subject
);
}
$subject = fGrammar::singularize($subject);
$plural = TRUE;
}
$subject = fGrammar::camelize($subject, TRUE);
if (isset($parameters[1])) {
return fORMRelated::associateRecords($class, $this->related_records, $subject, $parameters[0], $parameters[1]);
// This handles one-to-many and many-to-many relationships
if ($plural) {
return fORMRelated::associateRecords($class, $this->related_records, $subject, $records, $route);
}
return fORMRelated::associateRecords($class, $this->related_records, $subject, $parameters[0]);
// This handles one-to-one relationships
return fORMRelated::associateRecord($class, $this->related_records, $subject, $records, $route);
case 'build':
$subject = fGrammar::singularize($subject);
@@ -549,9 +570,9 @@ public function __call($method_name, $parameters)
$subject = fGrammar::camelize($subject, TRUE);
if (isset($parameters[0])) {
return fORMRelated::createRecord($class, $this->values, $subject, $parameters[0]);
return fORMRelated::createRecord($class, $this->values, $this->related_records, $subject, $parameters[0]);
}
return fORMRelated::createRecord($class, $this->values, $subject);
return fORMRelated::createRecord($class, $this->values, $this->related_records, $subject);
case 'inject':
$subject = fGrammar::singularize($subject);
@@ -581,13 +602,25 @@ public function __call($method_name, $parameters)
return fORMRelated::getPrimaryKeys($class, $this->values, $this->related_records, $subject);
case 'populate':
$subject = fGrammar::singularize($subject);
$subject = fGrammar::camelize($subject, TRUE);
$table = fORM::tablize($class);
$route = isset($parameters[0]) ? $parameters[0] : NULL;
if (isset($parameters[0])) {
return fORMRelated::populateRecords($class, $this->related_records, $subject, $parameters[0]);
// one-to-many relationships need to use plural forms
if (in_array($subject, fORMSchema::retrieve()->getTables())) {
if (fORMSchema::isOneToOne($table, $subject, $route)) {
throw new fProgrammerException(
'The table %1$s is not in a %2$srelationship with the table %3$s',
$table,
'one-to-many ',
$subject
);
}
$subject = fGrammar::singularize($subject);
}
return fORMRelated::populateRecords($class, $this->related_records, $subject);
$subject = fGrammar::camelize($subject, TRUE);
return fORMRelated::populateRecords($class, $this->related_records, $subject, $route);
case 'tally':
$subject = fGrammar::singularize($subject);
@@ -720,7 +753,7 @@ public function __construct($key=NULL)
// If the primary key does not look properly formatted, check to see if it is a UNIQUE key
$is_unique_key = FALSE;
if (is_array($key) && (sizeof($pk_columns) == 1 || array_keys($key) != $pk_columns)) {
if (is_array($key) && (sizeof($pk_columns) == 1 || array_diff(array_keys($key), $pk_columns))) {
$unique_keys = fORMSchema::retrieve()->getKeys($table, 'unique');
$key_keys = array_keys($key);
foreach ($unique_keys as $unique_key) {
@@ -730,7 +763,7 @@ public function __construct($key=NULL)
}
}
$wrong_keys = is_array($key) && array_keys($key) != $pk_columns;
$wrong_keys = is_array($key) && array_diff(array_keys($key), $pk_columns);
$wrong_type = !is_array($key) && (sizeof($pk_columns) != 1 || !is_scalar($key));
// If we didn't find a UNIQUE key and primary key doesn't look right we fail
Oops, something went wrong.

0 comments on commit fbe71f0

Please sign in to comment.