laminas / laminas-db

Database abstraction layer, SQL abstraction, result set abstraction, and RowDataGateway and TableDataGateway implementations
https://docs.laminas.dev/laminas-db/
BSD 3-Clause "New" or "Revised" License
122 stars 68 forks source link

Select from Combine #230

Open Jarkaruus opened 2 years ago

Jarkaruus commented 2 years ago

Bug Report

Q A
Version(s) 2.12.0

Summary

Writing the code below I obtain this error. If I create a workaround to class Combine, I obtain a wrong query like SELECT * FROM "( (SELECT * FROM table1) UNION (SELECT * FROM table12 UNION (SELECT * FROM table3) ) "AS i

Current behavior

'PHP message: PHP Recoverable fatal error: Object of class Laminas\\Db\\Sql\\Combine could not be converted to string laminas/laminas-db/src/Adapter/Platform/AbstractPlatform.php on line 75'

How to reproduce

$select = new Select();
$select->from(['i' => $combine]);

Expected behavior

I expect a query like SELECT FROM ( (SELECT FROM table1) UNION (SELECT FROM table12 UNION (SELECT FROM table3) ) AS i

LikeAJohny commented 11 months ago

Could you provide your Combine object so one can actually reproduce your error?

Jarkaruus commented 11 months ago

Of course, here it is:

$select1 = new \Laminas\Db\Sql\Select('articoli');
$select1->where->equalTo('id_articolo', 1);

$select2 = new \Laminas\Db\Sql\Select('articoli');
$select2->where->equalTo('id_articolo', 2);

$select3 = new \Laminas\Db\Sql\Select('articoli');
$select3->where->equalTo('id_articolo', 3);

$combine = new \Laminas\Db\Sql\Combine();
$combine->combine([$select1, $select2, $select3]);

$select = new \Laminas\Db\Sql\Select();
$select->from(['i' => $combine->getSqlString()]);

Below the results SELECT i. FROM `(SELECT articoli. FROM articoli WHERE id_articolo = '1') UNION (SELECT articoli. FROM articoli WHERE id_articolo = '2') UNION (SELECT articoli. FROM articoli WHERE id_articolo = '3')ASi`

Instead I expect something like: SELECT i. FROM ( (SELECT articoli. FROM articoli WHERE id_articolo = '1') UNION (SELECT articoli. FROM articoli WHERE id_articolo = '2') UNION (SELECT articoli. FROM articoli WHERE id_articolo = '3') ) AS i

LikeAJohny commented 8 months ago

Just realised I've never noticed your answer, I'm sorry.

I am not sure what you want to achieve with this specific way of using unions but I think what you want to do is this:

$select1 = new Select('articoli');
$select1->where->equalTo('id_articolo', 1);

$select2 = new Select('articoli');
$select2->where->equalTo('id_articolo', 2);
$select2->combine($select1);

$select3 = new Select('articoli');
$select3->where->equalTo('id_articolo', 3);
$select3->combine($select2);

$select = new Select();
$select->from(['i' => $select3]);

Given your use case I think the following would make more sense, though:

$select = new Select('articoli');
$select->where
    ->isEqualTo('id_articolo', 1)
    ->or
    ->isEqualTo('id_articolo', 2)
    ->or
    ->isEqualTo('id_articolo', 3);