Permalink
Browse files

BackwardsCompatibilityBreak - fRecordSet used to have ->preloadRelate…

…dRecords(), is now ->buildRelatedRecords(). Added ability to count related records from an active record by calling ->countRelatedRecords() on either an individual fActiveRecord, or if called on

an fRecordSet will load the counts for every record in the set.
  • Loading branch information...
wbond committed Jul 22, 2008
1 parent 5a17c6b commit 418540c053649a41099108d2853c2c4b9d2d819b
@@ -63,7 +63,7 @@ public function __call($method_name, $parameters)
}
// This will prevent quiet failure
if (($action == 'set' || $action == 'associate' || $action == 'inject') && sizeof($parameters) < 1) {
if (in_array($action, array('set', 'associate', 'inject', 'tally')) && sizeof($parameters) < 1) {
fCore::toss(
'fProgrammerException',
fGrammar::compose(
@@ -122,6 +122,15 @@ public function __call($method_name, $parameters)
}
return fORMRelated::constructRecordSet($this, $this->values, $this->related_records, $subject);
case 'count':
$subject = fGrammar::singularize($subject);
$subject = fGrammar::camelize($subject, TRUE);
if (isset($parameters[0])) {
return fORMRelated::countRecords($this, $this->values, $this->related_records, $subject, $parameters[0]);
}
return fORMRelated::countRecords($this, $this->values, $this->related_records, $subject);
case 'create':
$subject = fGrammar::camelize($subject, TRUE);
@@ -157,6 +166,15 @@ public function __call($method_name, $parameters)
}
return fORMRelated::populateRecords($this, $this->related_records, $subject);
case 'tally':
$subject = fGrammar::singularize($subject);
$subject = fGrammar::camelize($subject, TRUE);
if (isset($parameters[1])) {
return fORMRelated::tallyRecords($this, $this->related_records, $subject, $parameters[0], $parameters[1]);
}
return fORMRelated::tallyRecords($this, $this->related_records, $subject, $parameters[0]);
// Error handler
default:
fCore::toss(
@@ -708,8 +708,8 @@ static public function insertFromAndGroupByClauses($table, $sql, $joins=array())
$temp_sql = join('', $parts);
}
$temp_sql = str_replace(':from_clause', $from_clause, $temp_sql);
$temp_sql = str_replace(' :group_by_clause ', $group_by_clause, $temp_sql);
$temp_sql = str_replace(':from_clause', $from_clause, $temp_sql);
$temp_sql = preg_replace('#\s:group_by_clause\s#', $group_by_clause, $temp_sql);
}
$new_sql .= $temp_sql;
@@ -103,8 +103,8 @@ static public function constructRecordSet($class, &$values, &$related_records, $
$route = fORMSchema::getRouteName($table, $related_table, $route, '*-to-many');
// If we already have the sequence, we can stop here
if (isset($related_records[$related_table][$route])) {
return $related_records[$related_table][$route];
if (isset($related_records[$related_table][$route]['record_set'])) {
return $related_records[$related_table][$route]['record_set'];
}
$relationship = fORMSchema::getRoute($table, $related_table, $route, '*-to-many');
@@ -118,16 +118,66 @@ static public function constructRecordSet($class, &$values, &$related_records, $
$record_set = fRecordSet::create($related_class, $where_conditions, $order_bys);
}
// Cache the results for subsequent calls
if (!isset($related_records[$related_table])) {
$related_records[$related_table] = array();
}
$related_records[$related_table][$route] = $record_set;
self::setRecords($class, $related_records, $related_class, $record_set, $route);
return $record_set;
}
/**
* Counts the number of related one-to-many or many-to-many records
*
* @internal
*
* @param mixed $class The class name or instance of the class to get the related values for
* @param array &$values The values for the {@link fActiveRecord} class
* @param array &$related_records The related records existing for the {@link fActiveRecord} class
* @param string $related_class The class that is related to the current record
* @param string $route The route to follow for the class specified
* @return integer The number of related records
*/
static public function countRecords($class, &$values, &$related_records, $related_class, $route=NULL)
{
$table = fORM::tablize($class);
$related_table = fORM::tablize($related_class);
$route = fORMSchema::getRouteName($table, $related_table, $route, '*-to-many');
// If we already have the sequence, we can stop here
if (isset($related_records[$related_table][$route]['count'])) {
return $related_records[$related_table][$route]['count'];
}
$relationship = fORMSchema::getRoute($table, $related_table, $route, '*-to-many');
// Determine how we are going to build the sequence
if ($values[$relationship['column']] === NULL) {
$count = 0;
} else {
$column = $table . '.' . $relationship['column'];
$value = fORMDatabase::prepareBySchema($table, $relationship['column'], $values[$relationship['column']], '=');
$primary_keys = fORMSchema::getInstance()->getKeys($related_table, 'primary');
$primary_keys = fORMDatabase::addTableToValues($related_table, $primary_keys);
$primary_keys = join(', ', $primary_keys);
$sql = "SELECT count(" . $primary_keys . ") AS __flourish_count ";
$sql .= "FROM :from_clause ";
$sql .= "WHERE " . $column . $value;
$sql .= ' :group_by_clause ';
$sql .= 'ORDER BY ' . $column . ' ASC';
$sql = fORMDatabase::insertFromAndGroupByClauses($table, $sql);
$count = fORMDatabase::getInstance()->translatedQuery($sql)->fetchScalar();
}
self::tallyRecords($class, $related_records, $related_class, $count, $route);
return $count;
}
/**
* Figures out what filter to pass to {@link fRequest::filter()} for the specified related class
*
@@ -275,7 +325,7 @@ static public function overrideRelatedRecordName($class, $related_class, $record
if (!isset(self::$related_record_names[$table][$related_class])) {
self::$related_record_names[$table][$related_class] = array();
}
}
self::$related_record_names[$table][$related_class][$route] = $record_name;
}
@@ -388,7 +438,15 @@ static public function setRecords($class, &$related_records, $related_class, fRe
$route = fORMSchema::getRouteName($table, $related_table, $route, '*-to-many');
$related_records[$related_table][$route] = $records;
if (!isset($related_records[$related_table])) {
$related_records[$related_table] = array();
}
if (!isset($related_records[$related_table][$route])) {
$related_records[$related_table][$route] = array();
}
$related_records[$related_table][$route]['record_set'] = $records;
$related_records[$related_table][$route]['count'] = $records->count();
}
@@ -480,6 +538,37 @@ static public function storeManyToMany(&$values, $relationship, $record_set)
}
/**
* Records the number of related one-to-many or many-to-many records
*
* @internal
*
* @param mixed $class The class name or instance of the class to get the related values for
* @param array &$values The values for the {@link fActiveRecord} class
* @param array &$related_records The related records existing for the {@link fActiveRecord} class
* @param string $related_class The class that is related to the current record
* @param integer $count The number of records
* @param string $route The route to follow for the class specified
* @return void
*/
static public function tallyRecords($class, &$related_records, $related_class, $count, $route=NULL)
{
$table = fORM::tablize($class);
$related_table = fORM::tablize($related_class);
$route = fORMSchema::getRouteName($table, $related_table, $route, '*-to-many');
// Cache the results for subsequent calls
if (!isset($related_records[$related_table])) {
$related_records[$related_table] = array();
}
if (!isset($related_records[$related_table][$route])) {
$related_records[$related_table][$route] = array();
}
$related_records[$related_table][$route]['count'] = $count;
}
/**
* Forces use as a static class
*
Oops, something went wrong.

0 comments on commit 418540c

Please sign in to comment.