/** * Caches data about required objects fields in memory * * @param bool $all If true, caches required fields of all object classes. */ public function cacheFieldsRequiredDatabase($all = true) { if (!is_array(self::$fieldsRequiredDatabase)) { $fields = $this->getfieldsRequiredDatabase((bool) $all); if ($fields) { foreach ($fields as $row) { self::$fieldsRequiredDatabase[$row['object_name']][(int) $row['id_required_field']] = pSQL($row['field_name']); } } else { self::$fieldsRequiredDatabase = array(); } } }
/** * Build object * * @param int $id Existing object id in order to load object (optional) * @param int $id_lang Required if object is multilingual (optional) * @param int $id_shop ID shop for objects with multishop on langs */ public function __construct($id = null, $id_lang = null, $id_shop = null) { if (!ObjectModel::$db) { ObjectModel::$db = Db::getInstance(); } $this->def = self::getDefinition($this); $this->setDefinitionRetrocompatibility(); if ($id_lang !== null) { $this->id_lang = Language::getLanguage($id_lang) !== false ? $id_lang : Configuration::get('PS_LANG_DEFAULT'); } if ($id_shop && $this->isMultishop()) { $this->id_shop = (int) $id_shop; $this->get_shop_from_context = false; } if ($this->isMultishop() && !$this->id_shop) { $this->id_shop = Context::getContext()->shop->id; } if (!Validate::isTableOrIdentifier($this->def['primary']) || !Validate::isTableOrIdentifier($this->def['table'])) { throw new PrestaShopException('Identifier or table format not valid for class ' . get_class($this)); } if ($id) { // Load object from database if object id is present $cache_id = 'objectmodel_' . $this->def['table'] . '_' . (int) $id . '_' . (int) $id_shop . '_' . (int) $id_lang; if (!Cache::isStored($cache_id)) { $sql = new DbQuery(); $sql->from($this->def['table'], 'a'); $sql->where('a.' . $this->def['primary'] . ' = ' . (int) $id); // Get lang informations if ($id_lang) { $sql->leftJoin($this->def['table'] . '_lang', 'b', 'a.' . $this->def['primary'] . ' = b.' . $this->def['primary'] . ' AND b.id_lang = ' . (int) $id_lang); if ($this->id_shop && !empty($this->def['multilang_shop'])) { $sql->where('b.id_shop = ' . $this->id_shop); } } // Get shop informations if (Shop::isTableAssociated($this->def['table'])) { $sql->leftJoin($this->def['table'] . '_shop', 'c', 'a.' . $this->def['primary'] . ' = c.' . $this->def['primary'] . ' AND c.id_shop = ' . (int) $this->id_shop); } if ($row = ObjectModel::$db->getRow($sql)) { Cache::store($cache_id, $row); } } $result = Cache::retrieve($cache_id); if ($result) { $this->id = (int) $id; foreach ($result as $key => $value) { if (array_key_exists($key, $this)) { $this->{$key} = $value; } } if (!$id_lang && isset($this->def['multilang']) && $this->def['multilang']) { $sql = 'SELECT * FROM `' . pSQL(_DB_PREFIX_ . $this->def['table']) . '_lang` WHERE `' . $this->def['primary'] . '` = ' . (int) $id . ($this->id_shop && $this->isLangMultishop() ? ' AND `id_shop` = ' . $this->id_shop : ''); $result = ObjectModel::$db->executeS($sql); if ($result) { foreach ($result as $row) { foreach ($row as $key => $value) { if (array_key_exists($key, $this) && $key != $this->def['primary']) { if (!is_array($this->{$key})) { $this->{$key} = array(); } $this->{$key}[$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(); } } }
/** * 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 && Language::getLanguage((int) $id_lang)) { $this->id_lang = (int) $id_lang; } elseif ($id_lang != null) { $this->id_lang = _PS_LANG_DEFAULT_; } /* Connect to database and check SQL table/identifier */ if (!Validate::isTableOrIdentifier($this->identifier . $this->table)) { die(Tools::displayError()); } /* Load object from database if object id is present */ if ($id) { $this->identifier = pSQL($this->identifier); 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 && 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) && $key != $this->identifier) { if (!is_array($this->{$key})) { $this->{$key} = array(); } $this->{$key}[(int) $row['id_lang']] = $value; } } } } } } } if (!isset(self::$fieldsRequiredDatabase)) { self::$fieldsRequiredDatabase = array(); if ($fields = $this->getfieldsRequiredDatabase(true)) { foreach ($fields as $row) { self::$fieldsRequiredDatabase[$row['object_name']][(int) $row['id_required_field']] = pSQL($row['field_name']); } } } }
/** * 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(); } // Todo: stripslashes() MUST BE removed in 1.4.6 and later, but is kept in 1.4.5 for a compatibility issue $this->{$key}[(int) $row['id_lang']] = stripslashes($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(); } } }