E
E
E2018-02-12 09:24:59
symfony
E, 2018-02-12 09:24:59

How to request a OneToMany field using Doctrine?

...
/**
   * @ORM\OneToMany(targetEntity="App\Entity\ShiftBreak", mappedBy="shift")
   */
private $breaks;
...

...
 /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Shift", inversedBy="shifts")
     * @ORM\JoinColumn(name="shift_id", referencedColumnName="id")
     */
    private $shift;
...

Query
$this->createQueryBuilder('s')
->select([
    's.created',
    's.name',
    's.breaks'
])
->getQuery();

When I make this request, I get an error
[Semantical Error] line 0, col 99 near 'breaks FROM App\Entity\Shift': Error: Invalid PathExpression. Must be a StateFieldPathExpression.

If you remove 's.breaks' everything is OK, everything is displayed.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2018-02-12
@aylo

Your table (the "App\Entity\ShiftBreak" model) does not have the breaks field, so you cannot specify it in the select query.
To get data from 2 or more tables, you need to use JOIN:

$this->createQueryBuilder('s')
            ->select([
                's.created',
                's.name',
                'b.id'
            ])
            ->join('s.breaks', 'b')
            ->getQuery();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question