Skip to content

[QUESTION] Create mapper-subselect #7

@MigasEu

Description

@MigasEu

How can I create, from a MapperSelect, another MapperSelect to use in a join on the main query?

Right now I'm creating the 2 MapperSelects separated, then turning the second into a string and passing the bindvalues by hand.

The problem is that the bindings values colide, from the use of bindinline on both.

Something like:

/* @var $atlas \Atlas\Orm\Atlas */
$select = $atlas->select('table1')->(...);
$select2 = $atlas->select('table2')->(...);

$select2String = $select2->getStatement();
$select->join('INNER' , '('.$select2String.') as t2', 'table2.id = table.table2_id');
$bindValues = [];
foreach ($select2->getBindValues() as $bindKey => $bindValue) {
   $bindValues[$bindKey] = $bindValue[0];
}
$select->bindValues($bindValues);

Results in:

select
	*
from
	table1
inner join (
		select *
	from table2
	where
		table2.id = :__1__
		) t2 on t2.id = table1.table2_id
where
	table1.id = :__1__

Table1 has a relationship to table2, but instead of using a simple join, I want to use a subselect to keep separated scopes. This is useful especially because I'm reusing the 2nd select.

Thank you for the help.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions