Example #1
0
 public function get($column)
 {
     if (array_key_exists($column, $this->_object)) {
         return parent::get($column);
     } elseif (isset($this->_related[$column])) {
         // Return related model that has already been fetched
         return $this->_related[$column];
     } elseif (isset($this->_belongs_to[$column])) {
         return parent::get($column);
     } elseif (isset($this->_has_one[$column])) {
         return parent::get($column);
     } elseif (isset($this->_has_many[$column])) {
         $model = ORM::factory($this->_has_many[$column]['model']);
         if (isset($this->_has_many[$column]['through'])) {
             // Grab has_many "through" relationship table
             $through = $this->_has_many[$column]['through'];
             // Join on through model's target foreign key (far_key) and target model's primary key
             $join_col1 = $through . '.' . $this->_has_many[$column]['far_key'];
             $join_col2 = $model->_object_name . '.' . $model->_primary_key;
             $model->join($through)->on($join_col1, '=', $join_col2);
             // Through table's source foreign key (foreign_key) should be this model's primary key
             $col = $through . '.' . $this->_has_many[$column]['foreign_key'];
             $val = $this->pk();
         } else {
             if (Arr::get($this->_has_many[$column], 'polymorphic', FALSE) === TRUE && $model instanceof ORM_Polymorph) {
                 $col = $model->_object_name . '.' . $model->polymorph_id();
                 $val = $this->pk();
                 $model->where($model->polymorph_type(), '=', $this->_object_name);
             } else {
                 // Simple has_many relationship, search where target model's foreign key is this model's primary key
                 $col = $model->_object_name . '.' . $this->_has_many[$column]['foreign_key'];
                 $val = $this->pk();
             }
         }
         return $model->where($col, '=', $val);
     } else {
         return parent::get($column);
     }
 }