Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select from Combine #230

Open
Jarkaruus opened this issue Nov 13, 2021 · 3 comments
Open

Select from Combine #230

Jarkaruus opened this issue Nov 13, 2021 · 3 comments
Labels
Bug Something isn't working

Comments

@Jarkaruus
Copy link

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

@Jarkaruus Jarkaruus added the Bug Something isn't working label Nov 13, 2021
@LikeAJohny
Copy link

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

@Jarkaruus
Copy link
Author

Jarkaruus commented Nov 23, 2023

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') AS i

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants