コード例 #1
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);
 }