Esempio n. 1
0
 /**
  * Handles retrieval of all model values, relationships, and metadata.
  *
  * @param   string $column Column name
  * @return  mixed
  */
 public function __get($column)
 {
     if (array_key_exists($column, $this->_object)) {
         return in_array($column, $this->_serialize_columns) ? $this->_unserialize_value($this->_object[$column]) : $this->_object[$column];
     } elseif (isset($this->_related[$column])) {
         // Return related model that has already been fetched
         return $this->_related[$column];
     } elseif (isset($this->_belongs_to[$column])) {
         $model = $this->_related($column);
         // Use this model's column and foreign model's primary key
         $col = $model->_object_name . '.' . $model->_primary_key;
         $val = $this->_object[$this->_belongs_to[$column]['foreign_key']];
         // Make sure we don't run WHERE "AUTO_INCREMENT column" = NULL queries. This would
         // return the last inserted record instead of an empty result.
         // See: http://mysql.localhost.net.ar/doc/refman/5.1/en/server-session-variables.html#sysvar_sql_auto_is_null
         if ($val !== NULL) {
             $model->where($col, '=', $val)->find();
         }
         return $this->_related[$column] = $model;
     } elseif (isset($this->_has_one[$column])) {
         $model = $this->_related($column);
         // Use this model's primary key value and foreign model's column
         $col = $model->_object_name . '.' . $this->_has_one[$column]['foreign_key'];
         $val = $this->pk();
         $model->where($col, '=', $val)->find();
         return $this->_related[$column] = $model;
     } 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 {
             // 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 {
         echo get_class($this) . " has no " . $column;
         $e = new Kohana_Exception('The ' . $column . '  property does not exist in the ' . get_class($this) . ' class');
         $fh = fopen('/tmp/grisha.log', 'a');
         fwrite($fh, $e->getTraceAsString());
         fclose($fh);
         throw $e;
     }
 }