Esempio n. 1
0
 public function update($id)
 {
     if (!$this->find($id)) {
         return $this;
     }
     $this->model = $this->model->first();
     if ($this->versioned && !$this->multilanguage) {
         $latest_version = DB::table($this->table)->where_id($this->model->get_key())->max('version');
         $this->model->version = is_null($latest_version) ? 0 : $latest_version + 1;
         $this->model->exists = false;
     }
     if ($this->multilanguage) {
         if ($this->parent) {
             $this->input[$this->parent->language_table_foreign_key] = $this->parent->model->id;
             return $this->create();
         }
         $lang_input = array();
         foreach ($this->input['lang'] as $id => $input) {
             $input['language_id'] = $id;
             $input[$this->language_table_foreign_key] = $this->id;
             $lang_input[] = $input;
         }
         unset($this->input['lang']);
         $dal = DAL::model($this->language_model)->parent($this)->versioned($this->versioned)->input($lang_input)->update_multiple();
         if (!$dal->code == 200) {
             $this->data = $dal->data;
             $this->code = $dal->code;
             return $this;
         }
     } elseif (!is_null($this->slug) || isset($this->parent) && !is_null($this->parent)) {
         $key = isset($this->parent) && !is_null($this->parent) ? $this->parent->slug : $this->slug;
         $this->input['slug'] = Str::slug($this->input[$key]);
     }
     // Fill the model
     $this->model->fill($this->input);
     // Try to save
     if ($this->model->save() === false) {
         $this->code = 400;
         $this->data = (array) $this->model->errors->messages;
     }
     $this->sync();
     return $this;
 }