From 10247e8e4299beddbd8d506f4afe0ef46ae404e1 Mon Sep 17 00:00:00 2001 From: rMalie Date: Thu, 1 Dec 2011 14:47:28 +0000 Subject: [PATCH] [+] Classes: Add Collection class (synthesis of DbQuery + ObjectModel::hydrateCollection) and add support of static $definition property in ObjectModel git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@10834 b9a71923-0436-4b27-9f14-aed3839534dd --- classes/Collection.php | 181 ++++++++++++++++++++++++++++++++++++++++ classes/Gender.php | 15 ++-- classes/ObjectModel.php | 40 +++++++-- 3 files changed, 223 insertions(+), 13 deletions(-) create mode 100644 classes/Collection.php diff --git a/classes/Collection.php b/classes/Collection.php new file mode 100644 index 000000000..c6154bd91 --- /dev/null +++ b/classes/Collection.php @@ -0,0 +1,181 @@ + +* @copyright 2007-2011 PrestaShop SA +* @version Release: $Revision$ +* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) +* International Registered Trademark & Property of PrestaShop SA +*/ + +/** + * Create a collection of ObjectModel objects + * + * @since 1.5.0 + */ +class CollectionCore implements Iterator +{ + /** + * @var string Object class name + */ + protected $classname; + + /** + * @var int + */ + protected $id_lang; + + /** + * @var array Object definition + */ + protected $definition = array(); + + /** + * @var DbQuery + */ + protected $query; + + /** + * @var array Collection of objects in an array + */ + protected $results = array(); + + /** + * @var int Current object iteration + */ + protected $iterator = 0; + + /** + * @param string $classname + * @param int $id_lang + */ + public function __construct($classname, $id_lang = null) + { + $this->classname = $classname; + $this->id_lang = $id_lang; + + $this->definition = ObjectModel::getDefinition($this->classname); + if (!isset($this->definition['table'])) + throw new PrestashopException('Miss table in definition'); + + $this->query = new DbQuery(); + $this->query->select('a.*'); + $this->query->from($this->definition['table'].' a'); + + // If multilang, create assciation to lang table + if (isset($this->definition['multilang']) && $this->definition['multilang']) + { + $this->query->select('b.*'); + $this->query->leftJoin($this->definition['table'].'_lang b ON a.'.$this->definition['primary'].' = b.'.$this->definition['primary']); + } + } + + /** + * Add WHERE restriction on query + * + * @param string $str + * @return Collection + */ + public function where($str) + { + $this->query->where($str); + return $this; + } + + /** + * Add ORDER BY restriction on query + * + * @param string $str + * @return Collection + */ + public function orderBy($str) + { + $this->query->orderBy($str); + } + + /** + * Add GROUP BY restriction on query + * + * @param string $str + * @return Collection + */ + public function groupBy($str) + { + $this->query->groupBy($str); + } + + /** + * Launch sql query to create collection of objects + * + * @param bool $display_query If true, query will be displayed (for debug purpose) + * @return Collection + */ + public function getAll($display_query = false) + { + if ($display_query) + echo $this->query.'
'; + + $this->results = Db::getInstance()->executeS($this->query); + $this->results = ObjectModel::hydrateCollection($this->classname, $this->results, $this->id_lang); + + return $this; + } + + /** + * This method is called when a foreach begin + */ + public function rewind() + { + $this->iterator = 0; + $this->getAll(); + } + + /** + * Get current result + */ + public function current() + { + return $this->results[$this->iterator]; + } + + /** + * Check if there is a current result + */ + public function valid() + { + return isset($this->results[$this->iterator]); + } + + /** + * Get current result index + */ + public function key() + { + return $this->iterator; + } + + /** + * Go to next result + */ + public function next() + { + $this->iterator++; + } +} \ No newline at end of file diff --git a/classes/Gender.php b/classes/Gender.php index a8fe95828..5be2a1055 100644 --- a/classes/Gender.php +++ b/classes/Gender.php @@ -41,8 +41,11 @@ class GenderCore extends ObjectModel protected $fieldsSizeLang = array('name' => 20); protected $fieldsValidateLang = array('name' => 'isString'); - protected $table = 'gender'; - protected $identifier = 'id_gender'; + public static $definition = array( + 'table' => 'gender', + 'primary' => 'id_gender', + 'multilang' => true, + ); public function __construct($id = null, $id_lang = null, $id_shop = null) { @@ -77,12 +80,8 @@ class GenderCore extends ObjectModel if (is_null($id_lang)) $id_lang = Context::getContext()->language->id; - $sql = 'SELECT g.*, gl.* - FROM '._DB_PREFIX_.'gender g - LEFT JOIN '._DB_PREFIX_.'gender_lang gl ON g.id_gender = gl.id_gender AND gl.id_lang = '.(int)$id_lang; - $results = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql); - - return ObjectModel::hydrateCollection('Gender', $results, $id_lang); + $genders = new Collection('Gender', $id_lang); + return $genders; } public function getImage($use_unknown = false) diff --git a/classes/ObjectModel.php b/classes/ObjectModel.php index bc00067c9..9e3ddce63 100644 --- a/classes/ObjectModel.php +++ b/classes/ObjectModel.php @@ -37,11 +37,17 @@ abstract class ObjectModelCore private $getShopFromContext = true; - /** @var string SQL Table name */ - protected $table = null; + /** + * @var string SQL This property shouldn't be overloaded anymore in class, use static $definition['table'] property instead + * @deprecated + */ + protected $table; - /** @var string SQL Table identifier */ - protected $identifier = null; + /** + * @var string SQL This property shouldn't be overloaded anymore in class, use static $definition['primary'] property instead + * @deprecated + */ + protected $identifier; /** @var array Required fields for admin panel forms */ protected $fieldsRequired = array(); @@ -80,6 +86,12 @@ abstract class ObjectModelCore /** @var string file type of image files. Used for image deletion. */ protected $image_format = 'jpg'; + /** + * @var array Contain object definition + * @since 1.5.0 + */ + public static $definition = array(); + /** * Returns object validation rules (fields validity) * @@ -114,6 +126,15 @@ abstract class ObjectModelCore */ public function __construct($id = null, $id_lang = null, $id_shop = null) { + // For retrocompatibility, we continue to use $this->table and $this->identifier property. + // When all objects will implement $definition static in 1.6, we will remove it. + $definition = self::getDefinition($this); + if (isset($definition['table'])) + $this->table = $definition['table']; + if (isset($definition['primary'])) + $this->identifier = $definition['primary']; + + if (!is_null($id_lang)) $this->id_lang = (Language::getLanguage($id_lang) !== false) ? $id_lang : Configuration::get('PS_LANG_DEFAULT'); @@ -127,7 +148,7 @@ abstract class ObjectModelCore $this->id_shop = Context::getContext()->shop->getID(true); if (!Validate::isTableOrIdentifier($this->identifier) || !Validate::isTableOrIdentifier($this->table)) - throw new PrestashopException(Tools::displayError()); + throw new PrestashopException('Identifier or table format not valid'); if ($id) { @@ -1057,4 +1078,13 @@ abstract class ObjectModelCore } return $collection; } + + public static function getDefinition($class, $field = null) + { + $reflection = new ReflectionClass($class); + $definition = $reflection->getStaticPropertyValue('definition'); + if ($field) + return isset($definition[$field]) ? $definition[$field] : null; + return $definition; + } }