Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

join colums not reset #279

Open
hemengze opened this issue Nov 10, 2017 · 7 comments
Open

join colums not reset #279

hemengze opened this issue Nov 10, 2017 · 7 comments

Comments

@hemengze
Copy link

$this->columns = [];

@hemengze
Copy link
Author

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

@hemengze hemengze changed the title Left join colums not reset join colums not reset Nov 10, 2017
@froschdesign
Copy link
Member

@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;

@hemengze
Copy link
Author

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;

@froschdesign
Copy link
Member

@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

@hemengze
Copy link
Author

@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

@marc0n3
Copy link

marc0n3 commented May 16, 2018

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 😃

@michalbundyra
Copy link
Member

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants