/** * * @return Yada_Field_Foreign */ public function through() { var_dump('ManytoMany Through'); if (!$this->through instanceof Yada_Field_Foreign) { $init = NULL; if (is_array($this->through)) { list($model, $field) = $this->through; if (is_array($model)) { list($model, $init) = $model; } } elseif (is_string($this->through)) { $model = $this->through; $field = $this->related->name; } else { throw new Kohana_Exception('No through option specified for many-to-many field :field in model :model', array(':field' => $column, ':model' => Yada::common_name('model', $model))); } $model = Yada::model($model, $init); $dynamic = Yada::class_name('model', 'Dynamic'); if ($model instanceof $dynamic) { $this->_init_dynamic($model, $field); $this->meta->attach($model); } else { // Focus the through model and get the meta data $meta = $this->meta->meta($model); // Get the Yada Field Object that points back to this model $field = $meta->fields->{$field}; // Set that field's properties to point back to this model/field $field->related = $this; // Save the reference to that field $this->through = $field; } } // Focus the through model $this->meta->model($this->through); // return the through model return $this->through; }
/** * Return an array of stored model instances indexed by class name * @return array */ public function &models() { $result = array(); foreach ($this->_models as $model) { $name = Yada::class_name('model', $model); $result[$name] = isset($result[$name]) ? $result[$name] : array(); $result[$name][] = $model; } return $result; }