zend-db icon indicating copy to clipboard operation
zend-db copied to clipboard

join colums not reset

Open hemengze opened this issue 8 years ago • 7 comments

https://github.com/zendframework/zend-db/blob/748f065ab8e64ab66213182a671513e9499957bc/src/Sql/Select.php#L426

hemengze avatar Nov 10 '17 13:11 hemengze

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

hemengze avatar Nov 10 '17 13:11 hemengze

@hemengze The entire joins property is reset: https://github.com/zendframework/zend-db/blob/748f065ab8e64ab66213182a671513e9499957bc/src/Sql/Select.php#L428-L430

froschdesign avatar Nov 10 '17 13:11 froschdesign

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 avatar Nov 10 '17 13:11 hemengze

@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 avatar Nov 10 '17 14:11 froschdesign

@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

hemengze avatar Nov 10 '17 14:11 hemengze

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 :smiley:

marc0n3 avatar May 16 '18 08:05 marc0n3

This repository has been closed and moved to laminas/laminas-db; a new issue has been opened at https://github.com/laminas/laminas-db/issues/60.

michalbundyra avatar Jan 16 '20 19:01 michalbundyra