Используя shopProductsCollection::addJoin() добавил таблицы, но не получается вывести значения из них используя shopProductsCollection::getProducts($fields). Из $fields удаляются все поля не относящиеся к товару (p.*). Из-за этого также невозможно отсортировать данные по полям доп.таблиц.
Из описания shopProductsCollection::getSQL() следует что можно задать тип объединения используя поле 'type', но shopProductsCollection::addJoin() не позволяет задать его. Метод нужно дополнить:
/**
* Adds a simple JOIN clause to product selection query.
*
* @param string|array $table Table name to be used in JOIN clause.
* Alternatively an associative array may be specified containing values for all method parameters.
* In this case $on and $where parameters are ignored.
* @param string $on ON condition FOR JOIN, must not include 'ON' keyword
* @param string $where WHERE condition for SELECT, must not include 'WHERE' keyword
* @param string $type Type of join (LEFT, RIGHT, INNER)
* @return string Specified table's alias to be used in SQL query
*/
public function addJoin($table, $on = null, $where = null, $type = null)
{
if (is_array($table)) {
if (isset($table['on'])) {
$on = $table['on'];
}
if (isset($table['where'])) {
$where = $table['where'];
}
if (isset($table['type'])) {
$type = $table['type'];
}
$table = $table['table'];
}
$t = explode('_', $table);
$alias = '';
foreach ($t as $tp) {
if ($tp == 'shop') {
continue;
}
$alias .= substr($tp, 0, 1);
}
if (!$alias) {
$alias = $table;
}
if (!isset($this->join_index[$alias])) {
$this->join_index[$alias] = 1;
} else {
$this->join_index[$alias]++;
}
$alias .= $this->join_index[$alias];
$join = array(
'table' => $table,
'alias' => $alias,
);
if ($on) {
$join['on'] = str_replace(':table', $alias, $on);
}
if ($type) {
$join['type'] = $type;
}
$this->joins[] = $join;
if ($where) {
$this->where[] = str_replace(':table', $alias, $where);
}
return $alias;
}
/**
* Adds a LEFT JOIN clause to product selection query.
*
* @param string|array $table Table name to be used in JOIN clause.
* Alternatively an associative array may be specified containing values for all method parameters.
* In this case $on and $where parameters are ignored.
* @param string $on ON condition FOR JOIN, must not include 'ON' keyword
* @param string $where WHERE condition for SELECT, must not include 'WHERE' keyword
* @return string Specified table's alias to be used in SQL query
*/
public function addLeftJoin($table, $on = null, $where = null)
{
return $this->addJoin($table, $on, $where, 'LEFT');
}
3 комментария
Попутно всплыл баг в методе orderBy:
Мне не понятно при каких обстоятельствах сработает stock_worth.
Использование в методе $this->getSQL(); тоже не понятно чем обусловлено, почему не использовать $this->prepare()?
В итоге у меня получился вот такой код:
попутно добавил метод для добавления полей.