/** * Sets the related records for *-to-many relationships * * @internal * * @param string $class The class to set the related records for * @param array &$related_records The related records existing for the fActiveRecord class * @param string $related_class The class we are associating with the current record * @param fRecordSet $records The records are associating * @param string $route The route to use between the current class and the related class * @return void */ public static function setRecordSet($class, &$related_records, $related_class, fRecordSet $records, $route = NULL) { fActiveRecord::validateClass($related_class); fActiveRecord::forceConfigure($related_class); $table = fORM::tablize($class); $related_table = fORM::tablize($related_class); $schema = fORMSchema::retrieve($class); $route = fORMSchema::getRouteName($schema, $table, $related_table, $route); 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(); $related_records[$related_table][$route]['associate'] = FALSE; $related_records[$related_table][$route]['primary_keys'] = NULL; }
/** * Creates the objects for related records that are in a one-to-one or many-to-one relationship with the current class in a single DB query * * @param string $related_class This should be the name of a related class * @param string $route This should be the column name of the foreign key and is only required when there are multiple routes to a related table. If there are multiple routes and this is not specified, an fProgrammerException will be thrown. * @return fRecordSet The record set object, to allow for method chaining */ private function precreate($related_class, $route = NULL) { if (!$this->records) { return $this; } $this->validateSingleClass('precreate'); // If there are no primary keys we can just exit if (!array_merge($this->getPrimaryKeys())) { return $this; } fActiveRecord::validateClass($related_class); fActiveRecord::forceConfigure($related_class); $relationship = fORMSchema::getRoute(fORMSchema::retrieve($this->class), fORM::tablize($this->class), fORM::tablize($related_class), $route, '*-to-one'); $values = $this->call('get' . fGrammar::camelize($relationship['column'], TRUE)); $values = array_unique($values); self::build($related_class, array($relationship['related_column'] . '=' => $values)); return $this; }