public function initialize(Yada_Meta $meta, Yada_Model $model, $name, $alias) { parent::initialize($meta, $model, $name, $alias); if (!isset($this->expression)) { throw new Exception('You must specifiy an Expression for ' . Yada::common_name('model', $model) . '->' . $name); } }
public function related() { if (!$this->related instanceof Yada_Field_Interface_Related) { if (is_array($this->related) and count($this->related) == 2) { list($this->related, $field) = $this->related; } elseif (is_string($this->related)) { $field = $this->name; } else { throw new Kohana_Exception('Invalid related value for Field :field in Model :Model', array(':field' => $this->name, ':model' => Yada::common_name('model', $this->model))); } // Get the mapper and save it $this->mapper = $this->meta->mapper($this->model); // Focus the through model and get the meta data $meta = $this->meta->meta($this->related); // Get the Yada Field Object that points back to this model $field = $meta->fields->{$field}; $field->mapper = $meta->mapper; // Set that field's related to point back to this field $field->related = $this; // Save the reference to that field $this->related = $field; } // Focus the related model $this->meta->model($this->related); return $this->related; }
/** * * @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; }
public function related($action, $value) { var_dump('Related, model: ' . Yada::common_name('model', $this->_model)); var_dump('Related, action: ' . $action); $related = $this->_related(); $name = $this->_field->name; var_dump('Related, name: ' . $name); if (!$related->offsetExists($action)) { $related[$action] = array($name => $value); } else { $related[$action][$name] = $value; } $this->_state = 'changed'; return $this; }
/** * Sets the model's field values * * @param Yada_Model $model * @param string $name field name * @param mixed $value * @return mixed */ public function set_field(Yada_Model $model, $name, $value) { // Focus the model and get the Field's meta data $fields = $this->fields($model); // Throw error if field doesn't exist if (!$fields->offsetExists($name)) { throw new Kohana_Exception('Field :name doesn\'t exist in Model :model', array(':name' => $name, ':model' => Yada::common_name('model', $model))); } // Get the Yada_Field Object $field = $fields[$name]; $this->mapper()->field($model, $field)->set($value); return $value; }