zendframework/zend-db

join colums not reset

hemengze opened this issue · 7 comments

$this->columns = [];

case self::COLUMNS:
    $this->columns = array();
    foreach ($this->joins as &$join){
        $join['columns'] = [];
    }
break;

@hemengze
The entire joins property is reset:

zend-db/src/Sql/Select.php

Lines 428 to 430 in 748f065

case self::JOINS:
$this->joins = new Join;
break;

when work with paginator, sql like this:

select count(*), b.id, b.created_at from A as a inner join B as b on A.bid = b.id limit 1;

@hemengze

when work with paginator, sql like this:

Sorry, I don't understand. Please describe your problem with full sentence. This will help.
Thanks!

Maybe you mean this cloning:

https://github.com/zendframework/zend-paginator/blob/master/src/Adapter/DbSelect.php#L142-L145

@froschdesign thank you.
When I'm join a table with columns, reset the columns, the join columns still exists

$select = (new (Select))->join('B b', 'a.bid = b.id', 'id, created_at');
$select->reset(Select::COLUMNS);
$select->reset(Select::LIMIT);
$select->reset(Select::OFFSET);
$select->reset(Select::ORDER);

the columns b.id, b.created_at already exists

can something like this solve this problem?

public static function getCountSelect(Select $originalSelect, $fieldCountCriteria="id" )
	{
		$select = clone $originalSelect;
		$select->reset(Select::COLUMNS);
		$select->reset(Select::LIMIT);
		$select->reset(Select::OFFSET);
		$select->reset(Select::ORDER);
                $select->reset(Select::GROUP);
		/** @var Join $joins */
		$joins = $select->joins->getJoins();

		$select->reset(Select::JOINS);
		foreach ($joins as $key => $join) {
			//re add join without cols
			$type = $join["type"];
			$select->join($join["name"], $join["on"], [], $type);
		}

		$select->columns(["total" => new Expression("count(distinct($fieldCountCriteria))")]);

		return $select;
	}

It will re-init the joins without any column set.
It seems working for my situation... try at your own risk 😃

This repository has been closed and moved to laminas/laminas-db; a new issue has been opened at laminas/laminas-db#60.