コード例 #1
0
 /**
  * Will get the desired objects as long as they're configured correctly
  *
  * i.e. var $hasMany = array('Class Name' => 'fieldName in that table');
  * e.g. var $hasMany = array('Expense' => 'categoryID'); // would go in the ExpenseCategory object
  *
  * Has And Belongs To Many - $habtm - objects we're joined to via record_record or a join table
  *
  *				Special option keys for $additionalFinderOpts:
  *					'associations' - 	BOOL default false - if you only want the association objects and not the actual objects themselves pass this in
  *										*NOTE* applies only to record_record
  */
 public function get($getKey, $additionalFinderOpts = NULL)
 {
     $className = ucfirst($getKey);
     $internalVarName = self::INTERNAL_VAR_PREFIX . $className;
     if (!$this->{$internalVarName} || !is_null($additionalFinderOpts)) {
         // has many
         if (array_key_exists($className, $this->hasMany)) {
             if (!$this->id) {
                 return array();
             }
             $classNameToFind = $this->hasMany[$className]['className'] ? $this->hasMany[$className]['className'] : $className;
             $opts = array('where' => $this->hasMany[$className]['field'] . " = '" . $this->id . "'");
             if ($this->hasMany[$className]['condition']) {
                 $opts['where'] .= ' AND ' . $this->hasMany[$className]['condition'];
             }
             if (is_null($additionalFinderOpts)) {
                 $opts['order'] = $this->hasMany[$className]['order'];
                 if ($this->hasMany[$className]['finderOpts']) {
                     $opts = Finder::optionMerge($opts, $this->hasMany[$className]['finderOpts']);
                 }
                 $this->{$internalVarName} = Finder::factory($classNameToFind)->findAll($opts);
                 // cache this object in the found object
                 foreach ($this->{$internalVarName} as $foundObj) {
                     if (array_key_exists($this->getClass(), $foundObj->belongsTo)) {
                         $foundObj->set($this->getClass(), $this);
                     }
                 }
             } else {
                 $opts = Finder::optionMerge($opts, $additionalFinderOpts);
                 return Finder::factory($classNameToFind)->findAll($opts);
             }
             // belongs to
         } elseif (array_key_exists($className, $this->belongsTo)) {
             // this is a special keyword for tables with: className and recordID
             if ($className == 'Object') {
                 $this->{$internalVarName} = Finder::factory($this->className)->find($this->recordID);
             } else {
                 $foreignKey = $this->belongsTo[$className]['field'];
                 $classNameToFind = $this->belongsTo[$className]['className'] ? $this->belongsTo[$className]['className'] : $className;
                 $this->{$internalVarName} = Finder::factory($classNameToFind)->find($this->{$foreignKey});
             }
             // has and belongs to many
         } elseif (array_key_exists($className, $this->habtm)) {
             if (!$this->id()) {
                 return array();
             }
             // via the Associator
             if ($this->habtm[$className]['table'] == 'record_record') {
                 $classNameToFind = $this->habtm[$className]['className'] ? $this->habtm[$className]['className'] : $className;
                 $associator = new Associator();
                 $objectFinderOpts = array();
                 if ($this->habtm[$className]['condition']) {
                     $opts['where'] = $this->habtm[$className]['condition'];
                 }
                 if ($this->habtm[$className]['objectFinderOpts']) {
                     $objectFinderOpts = Finder::optionMerge($opts, $this->habtm[$className]['objectFinderOpts']);
                 }
                 // return only the associations, not the objects themselves
                 if ($additionalFinderOpts['associations']) {
                     $refObj = new ReflectionClass(ucfirst($className));
                     return $associator->findAssociations($refObj->getStaticPropertyValue('tableName'), $this->getTableName(), $this->id());
                 }
                 if ($additionalFinderOpts) {
                     $objectFinderOpts = Finder::optionMerge($additionalFinderOpts, $objectFinderOpts);
                 }
                 $this->{$internalVarName} = $associator->findAllObjectsForClassWithTableAndRecord($classNameToFind, $this->getTableName(), $this->id(), $opts, $objectFinderOpts);
                 // TODO: any join table
             } else {
                 $joinTable = $this->habtm[$className]['table'];
                 $associationForeignKey = $this->habtm[$className]['associationForeignKey'];
                 $foreignKey = $this->habtm[$className]['foreignKey'];
                 $ro = new ReflectionClass($className);
                 $instance = $ro->newInstance();
                 $tableName = $instance->getTableName();
                 $finder = new Finder($instance);
                 // more generic join tables
                 if (isset($this->habtm[$className]['typeKey']) && isset($this->habtm[$className]['typeValue'])) {
                     $typeKey = $this->habtm[$className]['typeKey'];
                     $typeValue = $this->habtm[$className]['typeValue'];
                     $additionalAnd = " AND jt." . $typeKey . "='{$typeValue}' ";
                 }
                 $sql = "SELECT activeRecord.* FROM {$joinTable} AS jt, {$tableName} as activeRecord\n\t\t\t\t\t\t\t\tWHERE jt." . $associationForeignKey . "=activeRecord.id\n\t\t\t\t\t\t\t\tAND activeRecord.siteID='" . $this->siteID . "'\n\t\t\t\t\t\t\t\tAND jt." . $foreignKey . "='" . $this->id . "'\n\t\t\t\t\t\t\t\t{$additionalAnd}";
                 $this->{$internalVarName} = $finder->findAllWithSql($sql, $additionalFinderOpts);
             }
             // try extras for ease
         } elseif ($this->hasExtra($getKey)) {
             return $this->getExtras($getKey);
             // error
         } else {
             //dump( $this->habtm );
             throw new Exception("<br/><br/>-----oh crap you haven't configured your objects correctly for CLASSNAME: " . $className . "---------<br/><br/>");
         }
     }
     return $this->{$internalVarName};
 }