Esempio n. 1
0
 /**
  * Set releated model, by assigning foreign key for this model
  * @param Jam_Validated $model
  * @param mixed         $value
  * @param boolean       $is_changed
  */
 public function set(Jam_Validated $model, $value, $is_changed)
 {
     if ($this->polymorphic_default_model and !$model->{$this->polymorphic}) {
         $model->{$this->polymorphic} = $this->polymorphic_default_model;
     }
     if (is_array($value) and $this->is_polymorphic()) {
         $model->{$this->polymorphic} = key($value);
         $value = current($value);
     } elseif ($value instanceof Jam_Model) {
         $model->{$this->polymorphic} = $value->meta()->model();
     }
     $key = Jam_Association::primary_key($this->foreign_model($model), $value);
     if (is_numeric($key) or $key === NULL) {
         $model->{$this->foreign_key} = $key;
     }
     if ($value instanceof Jam_Model and $this->inverse_of and $value->meta()->association($this->inverse_of) instanceof Jam_Association_Hasone) {
         $value->retrieved($this->inverse_of, $model);
     }
     return $value;
 }
Esempio n. 2
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();
     }
 }
Esempio n. 3
0
 /**
  * @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));
 }
Esempio n. 4
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)
 {
     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());
     }
 }