Пример #1
0
 /**
  * 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();
     }
 }
Пример #2
0
 /**
  * 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;
 }
Пример #3
0
 public function test_order_by()
 {
     $select = new Jam_Query_Builder_Update('test_post');
     $select->value('name', 'Test')->order_by('test_post.test_author_id', 'DESC');
     $this->assertEquals('UPDATE `test_posts` SET `name` = \'Test\' ORDER BY `test_posts`.`test_author_id` DESC', (string) $select);
 }