// Revert unfinished job on collections

This commit is contained in:
rMalie
2012-02-01 09:55:41 +00:00
parent b74e19afc7
commit d73305d528
+34 -81
View File
@@ -105,7 +105,7 @@ class CollectionCore implements Iterator, ArrayAccess, Countable
/** /**
* Join current entity to an associated entity * Join current entity to an associated entity
* *
* @param string $association Association name * @param $association Association name
* @param string $on * @param string $on
* @param int $type * @param int $type
* @return Collection * @return Collection
@@ -304,6 +304,10 @@ class CollectionCore implements Iterator, ArrayAccess, Countable
return $this; return $this;
$this->is_hydrated = true; $this->is_hydrated = true;
$alias = $this->generateAlias();
//$this->query->select($alias.'.*');
$this->query->from($this->definition['table'], $alias);
// If multilang, create association to lang table // If multilang, create association to lang table
if (!empty($this->definition['multilang'])) if (!empty($this->definition['multilang']))
{ {
@@ -312,39 +316,6 @@ class CollectionCore implements Iterator, ArrayAccess, Countable
$this->where(self::LANG_ALIAS.'.id_lang', '=', $this->id_lang); $this->where(self::LANG_ALIAS.'.id_lang', '=', $this->id_lang);
} }
// Build query
$this->query->from($this->definition['table'], $this->generateAlias());
// Add list of fields to select
$association_list = array('');
$association_list = array_merge($association_list, array_keys($this->join_list));
$fields_tree = array();
$fields = array();
$fields_alias = array();
foreach ($association_list as $association)
{
$def = $this->getDefinition($association);
$is_lang = !empty($def['is_lang']);
foreach ($def['fields'] as $field => $info)
if ((!$is_lang && empty($info['lang'])) || ($is_lang && !empty($info['lang'])))
{
$fields_tree[$association][] = $field;
if (!isset($fields[$field]))
$fields[$field] = $association;
else
{
if (!isset($fields_alias[$field]))
$fields_alias[$field] = array($fields[$field]);
$fields_alias[$field][] = $association;
}
}
}
$this->query->select('*');
foreach ($fields_alias as $field => $associations)
foreach ($associations as $association)
$this->query->select($this->generateAlias($association).'.'.$field.' as \''.$association.'.'.$field.'\'');
// Add join clause // Add join clause
foreach ($this->join_list as $data) foreach ($this->join_list as $data)
{ {
@@ -369,31 +340,8 @@ class CollectionCore implements Iterator, ArrayAccess, Countable
if ($display_query) if ($display_query)
echo $this->query.'<br />'; echo $this->query.'<br />';
// Get results and organize them $this->results = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($this->query);
/*$results = array(); $this->results = ObjectModel::hydrateCollection($this->classname, $this->results, $this->id_lang);
$query_ressource = Db::getInstance(_PS_USE_SQL_SLAVE_)->query($this->query);
while ($row = Db::getInstance(_PS_USE_SQL_SLAVE_)->nextRow($query_ressource))
{
$result = array();
foreach ($fields_tree as $association => $fields)
{
if ($association)
{
$ref = &$result;
foreach (explode('.', $association) as $asso)
{
if (!isset($ref['@associations'][$asso]))
$ref['@associations'][$asso] = array();
$ref = &$ref['@associations'][$asso];
$ref = &$ref[count($ref) - 1];
}
}
}
$results[] = $result;
}
d($results);*/
$this->results = ObjectModel::hydrateCollection($this->classname, Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($this->query), $this->id_lang);
return $this; return $this;
} }
@@ -549,23 +497,25 @@ class CollectionCore implements Iterator, ArrayAccess, Countable
if (!isset($this->association_definition[$association])) if (!isset($this->association_definition[$association]))
{ {
// Get previous association definition $definition = $this->definition;
$split = explode('.', $association); $split = explode('.', $association);
$asso = array_pop($split); $is_lang = false;
$previous_association = implode('.', $split); for ($i = 0, $total_association = count($split); $i < $total_association; $i++)
$definition = $this->getDefinition($previous_association);
// Check if association exists
if (!isset($definition['associations'][$asso]))
throw new PrestaShopException('Association '.$asso.' not found for class '.$definition['classname']);
$current_def = $definition['associations'][$asso];
// Special case for lang alias
if ($asso == self::LANG_ALIAS)
$is_lang = true;
else
{ {
$is_lang = false; $asso = $split[$i];
// Check is current association exists in current definition
if (!isset($definition['associations'][$asso]))
throw new PrestaShopException('Association '.$asso.' not found for class '.$this->definition['classname']);
$current_def = $definition['associations'][$asso];
// Special case for lang alias
if ($asso == self::LANG_ALIAS)
{
$is_lang = true;
break;
}
$classname = (isset($current_def['object'])) ? $current_def['object'] : Tools::toCamelCase($asso, true); $classname = (isset($current_def['object'])) ? $current_def['object'] : Tools::toCamelCase($asso, true);
$definition = ObjectModel::getDefinition($classname); $definition = ObjectModel::getDefinition($classname);
} }
@@ -574,22 +524,22 @@ class CollectionCore implements Iterator, ArrayAccess, Countable
$current_def['name'] = $asso; $current_def['name'] = $asso;
if (!isset($current_def['object'])) if (!isset($current_def['object']))
$current_def['object'] = Tools::toCamelCase($asso, true); $current_def['object'] = Tools::toCamelCase($asso, true);
if (!isset($current_def['field'])) if (!isset($current_def['field']))
$current_def['field'] = 'id_'.$asso; $current_def['field'] = 'id_'.$asso;
if (!isset($current_def['foreign_field'])) if (!isset($current_def['foreign_field']))
$current_def['foreign_field'] = 'id_'.$asso; $current_def['foreign_field'] = 'id_'.$asso;
if ($total_association > 1)
$current_def['complete_field'] = (($previous_association) ? '.' : '').$current_def['field']; {
unset($split[$total_association - 1]);
$current_def['complete_field'] = implode('.', $split).'.'.$current_def['field'];
}
else
$current_def['complete_field'] = $current_def['field'];
$current_def['complete_foreign_field'] = $association.'.'.$current_def['field']; $current_def['complete_foreign_field'] = $association.'.'.$current_def['field'];
$definition['is_lang'] = $is_lang; $definition['is_lang'] = $is_lang;
$definition['asso'] = $current_def; $definition['asso'] = $current_def;
$this->association_definition[$association] = $definition; $this->association_definition[$association] = $definition;
// Create join clause for this association
$this->join($association);
} }
else else
$definition = $this->association_definition[$association]; $definition = $this->association_definition[$association];
@@ -668,6 +618,9 @@ class CollectionCore implements Iterator, ArrayAccess, Countable
} }
$definition = $this->getDefinition($association); $definition = $this->getDefinition($association);
if ($association && !isset($this->join_list[$association]))
$this->join($association);
if ($fieldname == $definition['primary'] || (!empty($definition['is_lang']) && $fieldname == 'id_lang')) if ($fieldname == $definition['primary'] || (!empty($definition['is_lang']) && $fieldname == 'id_lang'))
$type = ObjectModel::TYPE_INT; $type = ObjectModel::TYPE_INT;
else else