/** * Build object * * @param integer $id Existing object id in order to load object (optional) * @param integer $id_lang Required if object is multilingual (optional) */ public function __construct($id = NULL, $id_lang = NULL) { if ($id_lang != NULL && Validate::isLoadedObject(new Language($id_lang))) { $this->id_lang = $id_lang; } elseif ($id_lang != NULL) { $this->id_lang = Configuration::get('PS_LANG_DEFAULT'); } /* Connect to database and check SQL table/identifier */ if (!Validate::isTableOrIdentifier($this->identifier) or !Validate::isTableOrIdentifier($this->table)) { die(Tools::displayError()); } $this->identifier = pSQL($this->identifier); /* Load object from database if object id is present */ if ($id) { if (!isset(self::$_cache[$this->table][(int) $id][(int) $id_lang])) { self::$_cache[$this->table][(int) $id][(int) $id_lang] = Db::getInstance()->getRow(' SELECT * FROM `' . _DB_PREFIX_ . $this->table . '` a ' . ($id_lang ? 'LEFT JOIN `' . pSQL(_DB_PREFIX_ . $this->table) . '_lang` b ON (a.`' . $this->identifier . '` = b.`' . $this->identifier . '` AND `id_lang` = ' . (int) $id_lang . ')' : '') . ' WHERE a.`' . $this->identifier . '` = ' . (int) $id); } $result = self::$_cache[$this->table][(int) $id][(int) $id_lang]; if ($result) { $this->id = (int) $id; foreach ($result as $key => $value) { if (key_exists($key, $this)) { $this->{$key} = $value; } } /* Join multilingual tables */ if (!$id_lang and method_exists($this, 'getTranslationsFieldsChild')) { $result = Db::getInstance()->ExecuteS(' SELECT * FROM `' . pSQL(_DB_PREFIX_ . $this->table) . '_lang` WHERE `' . $this->identifier . '` = ' . (int) $id); if ($result) { foreach ($result as $row) { foreach ($row as $key => $value) { if (key_exists($key, $this) and $key != $this->identifier) { if (!is_array($this->{$key})) { $this->{$key} = array(); } $this->{$key}[(int) $row['id_lang']] = $value; } } } } } } } if (!is_array(self::$fieldsRequiredDatabase)) { $fields = $this->getfieldsRequiredDatabase(true); if ($fields) { foreach ($fields as $row) { self::$fieldsRequiredDatabase[$row['object_name']][(int) $row['id_required_field']] = pSQL($row['field_name']); } } else { self::$fieldsRequiredDatabase = array(); } } }