/** * Assign default forein_model to singular association name * @param Jam_Meta $meta * @param string $name */ public function initialize(Jam_Meta $meta, $name) { if (!$this->foreign_model) { $this->foreign_model = Inflector::singular($name); } parent::initialize($meta, $name); }
/** * Save the related model before the main model, because we'll need the id to assign to the foreign key * Only save related model if it has been changed, and is not in a process of saving itself * @param Jam_Model $model * @param Jam_Event_Data $data * @param boolean $changed */ public function model_before_save(Jam_Model $model, Jam_Event_Data $data, $changed) { if ($value = Arr::get($changed, $this->name)) { if (Jam_Association::is_changed($value) and $item = $model->{$this->name}) { if (!$item->is_saving()) { $item->save(); } $this->set($model, $item, TRUE); } else { $this->set($model, $value, TRUE); } } }
/** * Save the related model after the main model, if it was changed * Only save related model if it has been changed, and is not in a process of saving itself * * @param Jam_Model $model * @param Jam_Event_Data $data * @param boolean $changed */ public function model_after_save(Jam_Model $model, Jam_Event_Data $data, $changed) { $nullify_query = $this->update_query($model, NULL, NULL); if ($value = Arr::get($changed, $this->name)) { if (Jam_Association::is_changed($value) and $item = $model->{$this->name}) { if (!$item->is_saving()) { $this->set($model, $item, TRUE)->save(); } if ($item->id()) { $nullify_query->where('id', '!=', $item->id())->execute(); } } else { $key = Jam_Association::primary_key($this->foreign_model, $value); $query = Jam_Query_Builder_Update::factory($this->foreign_model)->where(':unique_key', '=', $key)->value($this->foreign_key, $model->id()); if ($this->is_polymorphic()) { $query->value($this->polymorphic_key, $model->meta()->model()); } $nullify_query->execute(); $query->execute(); } } elseif (array_key_exists($this->name, $changed)) { $nullify_query->execute(); } }
/** * @covers Jam_Association::primary_key * @dataProvider data_primary_key */ public function test_primary_key($model_name, $value, $expected_primary_key) { $this->assertSame($expected_primary_key, Jam_Association::primary_key($model_name, $value)); }
/** * Save the related model after the main model, if it was changed * Only save related model if it has been changed, and is not in a process of saving itself * * @param Jam_Model $model * @param Jam_Event_Data $data * @param boolean $changed */ public function model_after_save(Jam_Model $model, Jam_Event_Data $data, $changed) { if ($value = Arr::get($changed, $this->name)) { if (Jam_Association::is_changed($value) and $item = $model->{$this->name}) { if (!$item->is_saving()) { $this->set($model, $item, TRUE)->save(); } $key = $item->id(); } else { $key = Jam_Association::primary_key($this->foreign_model, $value); } $this->erase_query($model)->execute($model->meta()->db()); $this->set_query($model, $key)->execute($model->meta()->db()); } }