Ejemplo n.º 1
0
 /**
  * @param  array $data
  * @return int
  * @throws Exception
  */
 protected function saveData($data)
 {
     if (!$this->isValidRequest('form')) {
         throw new Exception('Not valid request');
     }
     $session = new SessionNamespace($this->resource);
     if (empty($session->form->table)) {
         throw new Exception('Table empty');
     }
     $table = $session->form->table;
     if (!is_string($table)) {
         throw new Exception('Table parameter not string');
     }
     if (empty($session->form->primary_key)) {
         throw new Exception('Primary key empty');
     }
     $primary_key = $session->form->primary_key;
     if (!is_string($primary_key) && !is_numeric($primary_key)) {
         throw new Exception('Primary key not valid');
     }
     if (empty($data) || !is_array($data)) {
         throw new Exception('Data not valid');
     }
     $record_id = $session->form->record_id;
     if ($record_id) {
         $is_save = $this->db->update($table, $data, "{$primary_key} = {$record_id}");
     } else {
         $is_save = $this->db->insert($table, $data);
         $record_id = $this->db->lastInsertId($table);
     }
     if (!$is_save) {
         throw new Exception('Error save data');
     }
     if (isset($session->form->back_url)) {
         $this->response['back_url'] = $session->form->back_url;
     }
     $this->response['status'] = 'success';
     return $record_id;
 }