public function save($id_lang = 0) { if (!$this->id && !empty($this->object)) { $key = array(); $val = array(); $mark = array(); if ($this->_sortable) { $sp = $this->select(array('field' => 'sort_position', 'function' => 'MAX', 'as' => 'max'))->getRows(); $new_position = $sp[0]->max + 1; $this->object->sort_position = $new_position; } foreach ($this->object as $k => $v) { $key[] = $k; $val[] = $v; $mark[] = '?'; } try { $e = Db::getInstance()->prepare("INSERT INTO `" . _DB_PREFIX_ . $this::$table . "` (`" . implode('`,`', $key) . "`) values (" . implode(',', $mark) . ")"); $ret = $e->execute($val); $this->id = Db::getInstance()->lastInsertId(); $this->object->id = $this->id; if ($id_lang) { return $this->save($id_lang); } return $ret; } catch (\PDOException $e) { echo $e->getMessage(); } } else { if (!empty($this->object)) { if ($id_lang) { $lquery = Db::getInstance()->prepare("\n SELECT *\n FROM `" . _DB_PREFIX_ . static::$table . "_lang`\n WHERE `id_lang` = :ID_LANG AND `id_" . static::$table . "` = :ID\n "); $lquery->bindValue("ID_LANG", $id_lang, PDO::PARAM_INT); $lquery->bindValue("ID", $this->id, PDO::PARAM_INT); $lquery->execute(); $lang_fetch = $lquery->fetchObject(); if (!$lang_fetch) { $lang_fetch = new stdClass(); $lang_fetch->id_lang = $id_lang; $lang_fetch->{'id_' . \Tools::depluralize(static::$table)} = $this->id; $key = array(); $val = array(); $lmark = array(); foreach ($lang_fetch as $k => $v) { $lkey[] = $k; $lval[] = $v; $lmark[] = '?'; } $res = Db::getInstance()->prepare("INSERT INTO `" . _DB_PREFIX_ . $this::$table . "_lang` (`" . implode('`,`', $lkey) . "`) values (" . implode(',', $lmark) . ")")->execute($lval); $lang_fetch->id = Db::getInstance()->lastInsertId(); } // print_r($this->object); $lang_array = array(); foreach (static::$translated as $field) { // $this->object->{$field.'_'.$id_lang} = $this->object->{$field}; $lang_array[$field] = $this->object->{$field}; if (isset($this->object->{$field . '_0'})) { $this->object->{$field} = $this->object->{$field . '_0'}; } else { unset($this->object->{$field}); } unset($this->object->{$field . '_' . $id_lang}); unset($this->object->{$field . '_0'}); } } foreach ($this->object as $k => $v) { $key[] = "`" . $k . "`"; $val[] = $v; } $set = implode("=?,", $key) . "=?"; try { $query = Db::getInstance()->prepare("UPDATE `" . _DB_PREFIX_ . $this::$table . "` SET " . $set . " WHERE `id`='" . $this->id . "'"); if ($id_lang) { if ($query->execute($val)) { foreach (static::$translated as $field) { $lang_fetch->{$field} = $lang_array[$field]; $this->object->{$field} = $lang_array[$field]; } if ($lang_fetch) { $key = array(); $val = array(); foreach ($lang_fetch as $k => $v) { $key[] = "`" . $k . "`"; $val[] = $v; } $set = implode("=?,", $key) . "=?"; return Db::getInstance()->prepare("UPDATE `" . _DB_PREFIX_ . $this::$table . "_lang` SET " . $set . " WHERE `id`='" . $lang_fetch->id . "'")->execute($val); } } } else { //echo (int)$query->execute($val); return $query->execute($val); } } catch (\PDOException $e) { echo $e->getMessage(); } } } return false; }
public function drop() { $query_create = ' DROP TABLE IF EXISTS ' . $this->name; return Db::getInstance()->prepare($query_create)->execute(); }