// Fix lang bug on collections + use definitions on hydrateCollection
This commit is contained in:
+20
-1
@@ -91,6 +91,8 @@ class CollectionCore implements Iterator, ArrayAccess, Countable
|
||||
{
|
||||
$this->query->select('b.*');
|
||||
$this->query->leftJoin($this->definition['table'].'_lang b ON a.'.$this->definition['primary'].' = b.'.$this->definition['primary']);
|
||||
if ($this->id_lang)
|
||||
$this->query->where('b.id_lang = '.(int)$this->id_lang);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,6 +155,8 @@ class CollectionCore implements Iterator, ArrayAccess, Countable
|
||||
|
||||
/**
|
||||
* This method is called when a foreach begin
|
||||
*
|
||||
* @see Iterator::rewind()
|
||||
*/
|
||||
public function rewind()
|
||||
{
|
||||
@@ -162,6 +166,8 @@ class CollectionCore implements Iterator, ArrayAccess, Countable
|
||||
|
||||
/**
|
||||
* Get current result
|
||||
*
|
||||
* @see Iterator::current()
|
||||
*/
|
||||
public function current()
|
||||
{
|
||||
@@ -170,6 +176,9 @@ class CollectionCore implements Iterator, ArrayAccess, Countable
|
||||
|
||||
/**
|
||||
* Check if there is a current result
|
||||
*
|
||||
* @see Iterator::valid()
|
||||
* @return bool
|
||||
*/
|
||||
public function valid()
|
||||
{
|
||||
@@ -178,6 +187,9 @@ class CollectionCore implements Iterator, ArrayAccess, Countable
|
||||
|
||||
/**
|
||||
* Get current result index
|
||||
*
|
||||
* @see Iterator::key()
|
||||
* @return int
|
||||
*/
|
||||
public function key()
|
||||
{
|
||||
@@ -186,6 +198,8 @@ class CollectionCore implements Iterator, ArrayAccess, Countable
|
||||
|
||||
/**
|
||||
* Go to next result
|
||||
*
|
||||
* @see Iterator::next()
|
||||
*/
|
||||
public function next()
|
||||
{
|
||||
@@ -195,6 +209,7 @@ class CollectionCore implements Iterator, ArrayAccess, Countable
|
||||
/**
|
||||
* Get total of results
|
||||
*
|
||||
* @see Countable::count()
|
||||
* @return int
|
||||
*/
|
||||
public function count()
|
||||
@@ -206,6 +221,7 @@ class CollectionCore implements Iterator, ArrayAccess, Countable
|
||||
/**
|
||||
* Check if a result exist
|
||||
*
|
||||
* @see ArrayAccess::offsetExists()
|
||||
* @param $offset
|
||||
* @return bool
|
||||
*/
|
||||
@@ -218,6 +234,7 @@ class CollectionCore implements Iterator, ArrayAccess, Countable
|
||||
/**
|
||||
* Get a result by offset
|
||||
*
|
||||
* @see ArrayAccess::offsetGet()
|
||||
* @param $offset
|
||||
* @return ObjectModel
|
||||
*/
|
||||
@@ -232,6 +249,7 @@ class CollectionCore implements Iterator, ArrayAccess, Countable
|
||||
/**
|
||||
* Add an element in the collection
|
||||
*
|
||||
* @see ArrayAccess::offsetSet()
|
||||
* @param $offset
|
||||
* @param $value
|
||||
*/
|
||||
@@ -249,7 +267,8 @@ class CollectionCore implements Iterator, ArrayAccess, Countable
|
||||
|
||||
/**
|
||||
* Delete an element from the collection
|
||||
*
|
||||
*
|
||||
* @see ArrayAccess::offsetUnset()
|
||||
* @param $offset
|
||||
*/
|
||||
public function offsetUnset($offset)
|
||||
|
||||
+6
-19
@@ -1148,42 +1148,29 @@ abstract class ObjectModelCore
|
||||
throw new PrestashopException("Class '$class' not found");
|
||||
|
||||
$collection = array();
|
||||
$group_keys = array();
|
||||
$rows = array();
|
||||
$identifier = $fieldsValidateLang = null;
|
||||
if ($datas)
|
||||
{
|
||||
// Get identifier and lang fields
|
||||
$obj = new $class;
|
||||
if (!$obj instanceof ObjectModel)
|
||||
throw new PrestashopException("Class '$class' must be an instance of class ObjectModel");
|
||||
$identifier = $obj->getIdentifier();
|
||||
$fieldsValidateLang = $obj->getFieldsValidateLang();
|
||||
|
||||
// Check primary key
|
||||
if (!array_key_exists($identifier, $datas[0]))
|
||||
throw new PrestashopException("Identifier '$identifier' not found for class '$class'");
|
||||
$definition = ObjectModel::getDefinition($class);
|
||||
if (!array_key_exists($definition['primary'], $datas[0]))
|
||||
throw new PrestashopException("Identifier '{$definition['primary']}' not found for class '$class'");
|
||||
|
||||
foreach ($datas as $row)
|
||||
{
|
||||
// Get object common properties
|
||||
$id = $row[$identifier];
|
||||
$id = $row[$definition['primary']];
|
||||
if (!isset($rows[$id]))
|
||||
$rows[$id] = $row;
|
||||
|
||||
// Get object lang properties
|
||||
if (isset($row['id_lang']) && !$id_lang)
|
||||
{
|
||||
foreach ($fieldsValidateLang as $field => $validator)
|
||||
{
|
||||
if (!$id_lang)
|
||||
foreach ($definition['fields'] as $field => $data)
|
||||
if (!empty($data['lang']))
|
||||
{
|
||||
if (!is_array($rows[$id][$field]))
|
||||
$rows[$id][$field] = array();
|
||||
$rows[$id][$field][$row['id_lang']] = $row[$field];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user