[+] 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:
rMalie
2011-12-01 14:47:28 +00:00
parent 4e567f3d31
commit 10247e8e42
3 changed files with 223 additions and 13 deletions
+35 -5
View File
@@ -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;
}
}