Ejemplo n.º 1
0
 /**
  * Generates a clone of the current record, removing any auto incremented primary key value and allowing for replicating related records
  * 
  * This method will accept three different sets of parameters:
  * 
  *  - No parameters: this object will be cloned
  *  - A single `TRUE` value: this object plus all many-to-many associations and all child records (recursively) will be cloned
  *  - Any number of plural related record class names: the many-to-many associations or child records that correspond to the classes specified will be cloned
  * 
  * The class names specified can be a simple class name if there is only a
  * single route between the two corresponding database tables. If there is 
  * more than one route between the two tables, the class name should be
  * substituted with a string in the format `'RelatedClass{route}'`.
  * 
  * @param  string $related_class  The plural related class to replicate - see method description for details
  * @param  string ...
  * @return fActiveRecord  The cloned record
  */
 public function replicate($related_class = NULL)
 {
     fORM::callHookCallbacks($this, 'pre::replicate()', $this->values, $this->old_values, $this->related_records, $this->cache, fActiveRecord::$replicate_level);
     fActiveRecord::$replicate_level++;
     $class = get_class($this);
     $hash = self::hash($this->values, $class);
     $schema = fORMSchema::retrieve($class);
     $table = fORM::tablize($class);
     // If the object has not been replicated yet, do it now
     if (!isset(fActiveRecord::$replicate_map[$class])) {
         fActiveRecord::$replicate_map[$class] = array();
     }
     if (!isset(fActiveRecord::$replicate_map[$class][$hash])) {
         fActiveRecord::$replicate_map[$class][$hash] = clone $this;
         // We need the primary key to get a hash, otherwise certain recursive relationships end up losing members
         $pk_columns = $schema->getKeys($table, 'primary');
         if (sizeof($pk_columns) == 1 && $schema->getColumnInfo($table, $pk_columns[0], 'auto_increment')) {
             fActiveRecord::$replicate_map[$class][$hash]->values[$pk_columns[0]] = $this->values[$pk_columns[0]];
         }
     }
     $clone = fActiveRecord::$replicate_map[$class][$hash];
     $parameters = func_get_args();
     $recursive = FALSE;
     $many_to_many_relationships = $schema->getRelationships($table, 'many-to-many');
     $one_to_many_relationships = $schema->getRelationships($table, 'one-to-many');
     // When just TRUE is passed we recursively replicate all related records
     if (sizeof($parameters) == 1 && $parameters[0] === TRUE) {
         $parameters = array();
         $recursive = TRUE;
         foreach ($many_to_many_relationships as $relationship) {
             $parameters[] = fGrammar::pluralize(fORM::classize($relationship['related_table'])) . '{' . $relationship['join_table'] . '}';
         }
         foreach ($one_to_many_relationships as $relationship) {
             $parameters[] = fGrammar::pluralize(fORM::classize($relationship['related_table'])) . '{' . $relationship['related_column'] . '}';
         }
     }
     $record_sets = array();
     foreach ($parameters as $parameter) {
         // Parse the Class{route} strings
         if (strpos($parameter, '{') !== FALSE) {
             $brace = strpos($parameter, '{');
             $related_class = fGrammar::singularize(substr($parameter, 0, $brace));
             $related_class = fORM::getRelatedClass($class, $related_class);
             $related_table = fORM::tablize($related_class);
             $route = substr($parameter, $brace + 1, -1);
         } else {
             $related_class = fGrammar::singularize($parameter);
             $related_class = fORM::getRelatedClass($class, $related_class);
             $related_table = fORM::tablize($related_class);
             $route = fORMSchema::getRouteName($schema, $table, $related_table);
         }
         // Determine the kind of relationship
         $many_to_many = FALSE;
         $one_to_many = FALSE;
         foreach ($many_to_many_relationships as $relationship) {
             if ($relationship['related_table'] == $related_table && $relationship['join_table'] == $route) {
                 $many_to_many = TRUE;
                 break;
             }
         }
         foreach ($one_to_many_relationships as $relationship) {
             if ($relationship['related_table'] == $related_table && $relationship['related_column'] == $route) {
                 $one_to_many = TRUE;
                 break;
             }
         }
         if (!$many_to_many && !$one_to_many) {
             throw new fProgrammerException('The related class specified, %1$s, does not appear to be in a many-to-many or one-to-many relationship with %$2s', $parameter, get_class($this));
         }
         // Get the related records
         $record_set = fORMRelated::buildRecords($class, $this->values, $this->related_records, $related_class, $route);
         // One-to-many records need to be replicated, possibly recursively
         if ($one_to_many) {
             if ($recursive) {
                 $records = $record_set->call('replicate', TRUE);
             } else {
                 $records = $record_set->call('replicate');
             }
             $record_set = fRecordSet::buildFromArray($related_class, $records);
             $record_set->call('set' . fGrammar::camelize($route, TRUE), NULL);
         }
         // Cause the related records to be associated with the new clone
         fORMRelated::associateRecords($class, $clone->related_records, $related_class, $record_set, $route);
     }
     fActiveRecord::$replicate_level--;
     if (!fActiveRecord::$replicate_level) {
         // This removes the primary keys we had added back in for proper duplicate detection
         foreach (fActiveRecord::$replicate_map as $class => $records) {
             $table = fORM::tablize($class);
             $pk_columns = $schema->getKeys($table, 'primary');
             if (sizeof($pk_columns) != 1 || !$schema->getColumnInfo($table, $pk_columns[0], 'auto_increment')) {
                 continue;
             }
             foreach ($records as $hash => $record) {
                 $record->values[$pk_columns[0]] = NULL;
             }
         }
         fActiveRecord::$replicate_map = array();
     }
     fORM::callHookCallbacks($this, 'post::replicate()', $this->values, $this->old_values, $this->related_records, $this->cache, fActiveRecord::$replicate_level);
     fORM::callHookCallbacks($clone, 'cloned::replicate()', $clone->values, $clone->old_values, $clone->related_records, $clone->cache, fActiveRecord::$replicate_level);
     return $clone;
 }
Ejemplo n.º 2
0
 /**
  * Takes a table and turns it into a class name - uses custom mapping if set
  *
  * @param  string $table  The table name
  * @return string  The class name
  */
 public static function classize($table)
 {
     if (!($class = array_search($table, self::$class_table_map))) {
         $class = fGrammar::camelize(fGrammar::singularize($table), TRUE);
         self::$class_table_map[$class] = $table;
     }
     return $class;
 }
Ejemplo n.º 3
0
 /**
  * Allows for preloading various data related to the record set in single database queries, as opposed to one query per record
  * 
  * This method will handle methods in the format `verbRelatedRecords()` for
  * the verbs `build`, `prebuild`, `precount` and `precreate`.
  * 
  * `build` calls `create{RelatedClass}()` on each record in the set and
  * returns the result as a new record set. The relationship route can be
  * passed as an optional parameter.
  * 
  * `prebuild` builds *-to-many record sets for all records in the record
  * set. `precount` will count records in *-to-many record sets for every
  * record in the record set. `precreate` will create a *-to-one record
  * for every record in the record set.
  *  
  * @param  string $method_name  The name of the method called
  * @param  string $parameters   The parameters passed
  * @return void
  */
 public function __call($method_name, $parameters)
 {
     if ($callback = fORM::getRecordSetMethod($method_name)) {
         return call_user_func_array($callback, array($this, $this->class, &$this->records, $method_name, $parameters));
     }
     list($action, $subject) = fORM::parseMethod($method_name);
     $route = $parameters ? $parameters[0] : NULL;
     // This check prevents fGrammar exceptions being thrown when an unknown method is called
     if (in_array($action, array('build', 'prebuild', 'precount', 'precreate'))) {
         $related_class = fGrammar::singularize($subject);
         $related_class_sans_namespace = $related_class;
         if (!is_array($this->class)) {
             $related_class = fORM::getRelatedClass($this->class, $related_class);
         }
     }
     switch ($action) {
         case 'build':
             if ($route) {
                 $this->precreate($related_class, $route);
                 return $this->buildFromCall('create' . $related_class_sans_namespace, $route);
             }
             $this->precreate($related_class);
             return $this->buildFromCall('create' . $related_class_sans_namespace);
         case 'prebuild':
             return $this->prebuild($related_class, $route);
         case 'precount':
             return $this->precount($related_class, $route);
         case 'precreate':
             return $this->precreate($related_class, $route);
     }
     throw new fProgrammerException('Unknown method, %s(), called', $method_name);
 }
Ejemplo n.º 4
0
 /**
  * @dataProvider singularizePluralizeProvider
  */
 public function testSingularize($input, $output)
 {
     $this->assertEquals($output, fGrammar::singularize($input));
 }