// Norm + improve ObjectModel

This commit is contained in:
rMalie
2011-12-16 14:55:05 +00:00
parent c642a8b3c7
commit a33c19ce94
2 changed files with 41 additions and 57 deletions
+22 -37
View File
@@ -142,8 +142,9 @@ abstract class ObjectModelCore
/**
* Build object
*
* @param integer $id Existing object id in order to load object (optional)
* @param integer $id_lang Required if object is multilingual (optional)
* @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)
{
@@ -171,11 +172,15 @@ abstract class ObjectModelCore
$cache_id = 'objectmodel_'.$this->def['table'].'_'.(int)$id.'_'.(int)$id_shop.'_'.(int)$id_lang;
if (!Cache::isStored($cache_id))
{
$sql = 'SELECT *
FROM `'._DB_PREFIX_.$this->def['table'].'` a '.
($id_lang ? ('LEFT JOIN `'.pSQL(_DB_PREFIX_.$this->def['table']).'_lang` b ON (a.`'.$this->def['primary'].'` = b.`'.$this->def['primary']).'` AND `id_lang` = '.(int)($id_lang).')' : '')
.' WHERE 1 AND a.`'.$this->def['primary'].'` = '.(int)$id.
(($this->id_shop AND $id_lang) ? ' AND b.id_shop = '.$this->id_shop : '');
$sql = new DbQuery();
$sql->from($this->def['table'].' a');
$sql->where('a.'.$this->def['primary'].' = '.(int)$id);
if ($id_lang)
{
$sql->leftJoin($this->def['table'].'_lang b ON a.'.$this->def['primary'].' = b.'.$this->def['primary'].' AND b.id_lang = '.(int)$id_lang);
if ($this->id_shop)
$sql->where('b.id_shop = '.$this->id_shop);
}
Cache::store($cache_id, Db::getInstance()->getRow($sql));
}
@@ -1086,17 +1091,19 @@ abstract class ObjectModelCore
}
/**
* Specify if an ObjectModel is already in database
*
* @param $id_entity entity id
* @return boolean
*/
* Specify if an ObjectModel is already in database
*
* @param int $id_entity
* @param string $table
* @return boolean
*/
public static function existsInDatabase($id_entity, $table)
{
$row = Db::getInstance()->getRow('
SELECT `id_'.$table.'` as id
FROM `'._DB_PREFIX_.$table.'` e
WHERE e.`id_'.$table.'` = '.(int)($id_entity));
SELECT `id_'.$table.'` as id
FROM `'._DB_PREFIX_.$table.'` e
WHERE e.`id_'.$table.'` = '.(int)$id_entity
);
return isset($row['id']);
}
@@ -1118,28 +1125,6 @@ abstract class ObjectModelCore
return (bool)Db::getInstance()->getValue($query);
}
/**
* Get object identifier name
*
* @since 1.5.0
* @return string
*/
public function getIdentifier()
{
return $this->def['primary'];
}
/**
* Get list of fields related to language to validate
*
* @since 1.5.0
* @return array
*/
public function getFieldsValidateLang()
{
return $this->fieldsValidateLang;
}
/**
* Fill an object with given data. Data must be an array with this syntax: array(objProperty => value, objProperty2 => value, etc.)
*
+19 -20
View File
@@ -114,6 +114,8 @@ abstract class DbCore
/**
* Get number of rows in a result
*
* @param mixed $result
*/
abstract protected function _numRows($result);
@@ -129,6 +131,8 @@ abstract class DbCore
/**
* Get next row for a query which doesn't return an array
*
* @param mixed $result
*/
abstract public function nextRow($result = false);
@@ -248,11 +252,12 @@ abstract class DbCore
* @param string $data Data to insert/update
* @param string $type INSERT or INSERT IGNORE or UPDATE
* @param string $where WHERE clause, only for UPDATE (optional)
* @param string $limit LIMIT clause (optional)
* @param int $limit LIMIT clause (optional)
* @param bool $use_cache
* @param bool $use_null If true, replace empty strings and NULL by a NULL value
* @return mixed|boolean SQL query result
*/
public function autoExecute($table, $data, $type, $where = false, $limit = false, $use_cache = 1, $use_null = false)
public function autoExecute($table, $data, $type, $where = '', $limit = 0, $use_cache = true, $use_null = false)
{
if (!$data)
return true;
@@ -330,10 +335,10 @@ abstract class DbCore
* @param string $values Data to insert/update
* @param string $type INSERT or UPDATE
* @param string $where WHERE clause, only for UPDATE (optional)
* @param string $limit LIMIT clause (optional)
* @param int $limit LIMIT clause (optional)
* @return mixed|boolean SQL query result
*/
public function autoExecuteWithNullValues($table, $values, $type, $where = false, $limit = false)
public function autoExecuteWithNullValues($table, $values, $type, $where = '', $limit = 0)
{
return $this->autoExecute($table, $values, $type, $where, $limit, 0, true);
}
@@ -364,7 +369,7 @@ abstract class DbCore
* @param bool $use_cache Use cache or not
* @return bool
*/
public function delete($table, $where = false, $limit = false, $use_cache = true)
public function delete($table, $where = '', $limit = 0, $use_cache = true)
{
$this->result = false;
$sql = 'DELETE FROM `'.bqSQL($table).'`'.($where ? ' WHERE '.$where : '').($limit ? ' LIMIT '.(int)$limit : '');
@@ -397,7 +402,7 @@ abstract class DbCore
*
* @param string $sql query to execute
* @param boolean $array return an array instead of a mysql_result object (deprecated since 1.5.0, use query method instead)
* @param int $use_cache if query has been already executed, use its result
* @param bool $use_cache if query has been already executed, use its result
* @return array or result object
*/
public function executeS($sql, $array = true, $use_cache = true)
@@ -443,7 +448,7 @@ abstract class DbCore
* This function automatically add "limit 1" to the query
*
* @param mixed $sql the select query (without "LIMIT 1")
* @param int $use_cache find it in cache first
* @param bool $use_cache find it in cache first
* @return array associative array of (field=>value)
*/
public function getRow($sql, $use_cache = true)
@@ -475,8 +480,8 @@ abstract class DbCore
* getValue return the first item of a select query.
*
* @param mixed $sql
* @param int $use_cache
* @return void
* @param bool $use_cache
* @return mixed
*/
public function getValue($sql, $use_cache = true)
{
@@ -512,11 +517,10 @@ abstract class DbCore
*
* @param string $sql
* @param bool $use_cache
* @return mixed $result
*/
protected function q($sql, $use_cache = true)
{
global $webservice_call;
if ($sql instanceof DbQuery)
$sql = $sql->build();
@@ -530,7 +534,7 @@ abstract class DbCore
/**
* Display last SQL error
*
* @param unknown_type $sql
* @param bool $sql
*/
public function displayError($sql = false)
{
@@ -575,7 +579,8 @@ abstract class DbCore
* @param string $user Login for database connection
* @param string $pwd Password for database connection
* @param string $db Database name
* @param bool $newDbLink
* @param bool $new_db_link
* @param bool $engine
* @return int
*/
public static function checkConnection($server, $user, $pwd, $db, $new_db_link = true, $engine = null)
@@ -596,13 +601,7 @@ abstract class DbCore
return call_user_func_array(array(Db::getClass(), 'tryUTF8'), array($server, $user, $pwd));
}
/**
* Alias of Db::getInstance()->ExecuteS
*
* @acces string query The query to execute
* @return array Array of line returned by MySQL
*/
public static function s($sql, $use_cache = 1)
public static function s($sql, $use_cache = true)
{
return Db::getInstance()->executeS($sql, true, $use_cache);
}