Giter Site home page Giter Site logo

Feature Request: Add subquery support about orm HOT 8 OPEN

cycle avatar cycle commented on August 22, 2024
Feature Request: Add subquery support

from orm.

Comments (8)

mrakolice avatar mrakolice commented on August 22, 2024 1
$repository->select()->subquery([
    'myAwesomeAlias' => function(QueryBuilder $qb){
        $qb->from($tableName)->columns('field1', 'field2');
    }
]);

SELECT myAwesomeAlias.field1, myAwesomeAlias.field2, related.*
FROM (
SELECT table1.field1, table1.field2
FROM table1
WHERE table1.field3 = 'value'
LIMIT 10 OFFSET 10
) as myAwesomeAlias
LEFT JOIN related ON related.table1_id = some_alias.field1
$repository->select()->subquery(function(QueryBuilder $qb){
        $qb->from($tableName)->columns('field1', 'field2')->
                  where(['field3'=>'value'])->limit(10)->offset(10);
    })->load('related', ['method'=>LEFT_JOIN, 'load'=>...]);

SELECT some_alias.field1, some_alias.field2, related.*
FROM (
SELECT table1.field1, table1.field2
FROM table1
WHERE table1.field3 = 'value'
LIMIT 10 OFFSET 10
) as some_alias
LEFT JOIN related ON related.table1_id = some_alias.field1

If not existed $qb->from($tableName), then by default $tableName is current tableName in select.

$anotherSelect = $table3Repository->select()->columns(['field1' => 'some_field1', 'field2' => 'some_field2']);

$table1Repository->select()->subquery(function(QueryBuilder $qb) use($anotherSelect){
    $qb->addSelect($anotherSelect);
});

SELECT some_alias.field1, some_alias.field2
FROM (
SELECT table3.some_field1 AS field1, table3.some_field2 AS field2
FROM table3
) as some_alias

But this select is going to resolve table1Entity fields. This behaviour can be useful for materialized views, I think, or something like that. Or just for refactoring/customization purposes.

from orm.

wolfy-j avatar wolfy-j commented on August 22, 2024

Add possibility for generating subqueries in core
Already exist in DBAL.

Sync column aliases from subquery or parent query
Already exists in DBAL.

Need to create new fetch method though.

from orm.

mrakolice avatar mrakolice commented on August 22, 2024

If you provide some information about it, I'll do merge request tomorrow.

from orm.

wolfy-j avatar wolfy-j commented on August 22, 2024

Honestly, that's something for me personally. Take a look at Select/Loader functionality first.

from orm.

wolfy-j avatar wolfy-j commented on August 22, 2024

Can you give examples of sql produced? I can help with pin-pointing the location.

from orm.

wolfy-j avatar wolfy-j commented on August 22, 2024

Try $select->from($insertQueryHere). How close is that to what you want, if it is we will make better syntax for it. Shouldn't be hard.

from orm.

mrakolice avatar mrakolice commented on August 22, 2024

Yeah, this is some hack issue for me, but it's works.
Problem with this solution - you need to interpolate the query and manually resolve table aliases.

$select = $orm->getRepository()->select();

$params = new QueryParameters();
$sqlStatement = $select->buildQuery()->sqlStatement($params);

$sqlStatement = Interpolator::interpolate($sqlStatement, $params->getParameters());

$resultStatement = preg_replace('/\sAS\s\"c\d+\"/', '', $sqlStatement);

$select = $select->from(
    (new Fragment('(' . $resultStatement . ') as "' .
                $select->getBuilder()->getLoader()->getAlias() . '"'))
);

from orm.

wolfy-j avatar wolfy-j commented on August 22, 2024

Good, so this is the baseline.

from orm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.