无法通过Join添加select()

时间:2018-11-29 15:54:53

标签: mysql symfony doctrine-orm doctrine

我有一个使用Doctrine(和PHP Symfony框架)的查询-数组结果

$qb = $this->createQueryBuilder('t1', 't1.id')
     ->select('t1')
     ->where(...)
     ->getResults()

    // Return :

    158 => [
        "id" => 158
        "name" => "Test 1"
    ]
    157 => [
        "id" => 157
        "name" => "Test 2"
    ]

我想加入一个表:

$qb->leftJoin(..., 't2', 'WITH', 't2 = ...');

我成功了两种情况(lastPost表中没有t1字段; 我只想加入并获取字段):

$qb->addSelect('t2.id AS t0.lastPost')

    // Return :

    158 => [
        0 => [
            "id" => 158
            "name" => "Test 1"
        ],
        "lastPost" => 161
    ]
    157 => [
        0 => [
            "id" => 157
            "name" => "Test 2"
        ],
        "lastPost" => 163
    ]

和:

$qb->addSelect('t2 AS t0.lastPost')

    // Return :

    158 => [
        0 => [
            "id" => 158
            "name" => "Test 1"
        ]
    ]
    159 => [
        "lastPost" => [
            "id" => 161,
            "name" => "Test 3"
        ]
    ]
    157 => [
        0 => [
            "id" => 157
            "name" => "Test 2"
        ]
    ]
    160 => [
        "lastPost" => [
            "id" => 163,
            "name" => "Test 4"
        ]
    ]

但是我无法将lastPost添加到原始数组中,例如:

    158 => [
        0 => [
            "id" => 158
            "name" => "Test 1"
        ],
        "lastPost" => [
            "id" => 161,
            "name" => "Test 3"
        ]
    ]
    157 => [
        0 => [
            "id" => 157
            "name" => "Test 2"
        ],
        "lastPost" => [
            "id" => 163,
            "name" => "Test 4"
        ]
    ]

您能帮我意识到吗? 对不起,我的英语不好,我是法语:)

0 个答案:

没有答案