Exemplo n.º 1
0
 public function save()
 {
     if ($this->dirtyValue) {
         $oldIds = $this->getIds();
         $ids = [];
         foreach ($this->dirtyValue as $obj) {
             if ($obj->exists) {
                 $ids[] = $obj->getKey();
             }
             $this->relation->save($obj);
         }
         $toUnlink = array_diff($oldIds, $ids);
     } else {
         $toUnlink = $this->getIds();
     }
     if ($toUnlink && is_array($toUnlink)) {
         foreach ($toUnlink as $id) {
             $col_id = $this->relation->getForeignKey();
             $col_class = $this->relation->getPlainMorphType();
             $obj = CrudModel::createInstance($this->config['model'], null, $id);
             if (($this->config['on_delete'] ?? false) === 'delete') {
                 $obj->delete();
             } else {
                 $obj->{$col_id} = null;
                 $obj->{$col_class} = null;
                 $obj->save();
             }
         }
     }
 }
Exemplo n.º 2
0
 public function save()
 {
     if (is_array($this->dirtyValue)) {
         $oldIds = $this->getIds();
         foreach ($this->dirtyValue as $id) {
             $obj = CrudModel::createInstance($this->config['model'], null, $id);
             $this->relation->save($obj);
         }
         $toUnlink = array_diff($oldIds, $this->dirtyValue);
     } else {
         $toUnlink = $this->getIds();
     }
     if ($toUnlink && is_array($toUnlink)) {
         foreach ($toUnlink as $id) {
             $col = $this->relation->getForeignKey();
             //                if (!empty($field['ref_column']))
             //                {
             //                    $col = $field['ref_column'];
             //                }
             //                else
             //                {
             //                    $col = $this->classViewName . '_id';
             //                }
             $obj = CrudModel::createInstance($this->config['model'], null, $id);
             if (($this->config['on_delete'] ?? false) === 'delete') {
                 $obj->delete();
             } else {
                 $obj->{$col} = null;
                 $obj->save();
             }
         }
     }
 }
Exemplo n.º 3
0
 public function get()
 {
     $val = parent::get();
     if (is_null($val)) {
         return CrudModel::createInstance($this->config['model']);
     }
     return $val;
 }
Exemplo n.º 4
0
 public function createRelatedModel()
 {
     return CrudModel::createInstance($this->config['model']);
 }
Exemplo n.º 5
0
 public function save()
 {
     $obj = CrudModel::createInstance($this->config['model'], null, $this->dirtyValue);
     $obj->setAttribute($this->config['field'], $this->model->getKey());
     $obj->save();
 }