[+] 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
This commit is contained in:
@@ -0,0 +1,181 @@
|
||||
<?php
|
||||
/*
|
||||
* 2007-2011 PrestaShop
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Open Software License (OSL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://opensource.org/licenses/osl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
|
||||
* versions in the future. If you wish to customize PrestaShop for your
|
||||
* needs please refer to http://www.prestashop.com for more information.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @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.'<br />';
|
||||
|
||||
$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++;
|
||||
}
|
||||
}
|
||||
+7
-8
@@ -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)
|
||||
|
||||
+35
-5
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user