public function test_constructor() { $select = Jam_Query_Builder_Update::factory('test_author'); $select->value('name', 'Test'); $this->assertInstanceOf('Jam_Query_Builder_Update', $select); $this->assertEquals(Jam::meta('test_author'), $select->meta()); $this->assertEquals('UPDATE `test_authors` SET `name` = \'Test\'', (string) $select); }
/** * Generate a query to add models from the association (without deleting them), for specific ids * @param Jam_Model $model * @param array $ids * @return Database_Query */ public function add_items_query(Jam_Model $model, array $ids) { $query = Jam_Query_Builder_Update::factory($this->foreign_model)->where(':primary_key', 'IN', $ids)->value($this->foreign_key, $model->id()); if ($this->is_polymorphic()) { $query->value($this->polymorphic_key, $this->model); } return $query; }
/** * 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(); } }