Click here to Skip to main content
15,567,221 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
have a question about left join in doctrine in Symfony 2.7.

Example code:

PHP
public function test($id, $offset, $limit)
{
    $build = $this->createQueryBuilder('building');
    $build
        ->addSelect('users', 'numbers')
        ->join('building.users', 'users')
        // limit the numbers for 1 result!
        ->leftJoin('building.numbers', 'numbers') // only select 1 result instead of more.
        ->where('building.id = :id')
        ->setParameter('id', $id);

    $paginator = new Paginator($build->getQuery(), $fetchJoinCollection = true);
    $result = $paginator->getQuery()
        ->setFirstResult($offset)
        ->setMaxResults($limit)
        ->getResult();

    return $result;
}


My question now is how could we implement that the ->leftJoin('building.numbers', 'numbers') only return MAX 1 result.

Thanks!

Doctrine orm: 2.2.3, Symfony version: 2.7

What I have tried:

PHP
public function test($id, $offset, $limit)
{
    $build = $this->createQueryBuilder('building');
    $build
        ->addSelect('users', 'numbers')
        ->join('building.users', 'users')
        // limit the numbers for 1 result!
        ->leftJoin('building.numbers', 'numbers') // only select 1 result instead of more.
        ->where('building.id = :id')
        ->setParameter('id', $id);

    $paginator = new Paginator($build->getQuery(), $fetchJoinCollection = true);
    $result = $paginator->getQuery()
        ->setFirstResult($offset)
        ->setMaxResults($limit)
        ->getResult();

    return $result;
}
Posted
Comments
Maciej Los 6-Feb-18 8:54am    
I'm not a Symfony expert, but seems you want to join more then 1 table. You have to define join specification for each table. See: symfony - Mixing LEFT JOIN results MySQL Doctrine - Stack Overflow[^]
More about doctrines, you'll find here: How to Work with Doctrine Associations / Relations (Symfony Docs)[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900