Empty child items in Symfony entity after saving, but data is in database

I have a problem (or I’m doing something wrong) with Symfony 4.4. I have two entities, Item and LineItem. LineItem is a child entity of Item (relation ManyToOne).

If I create a new Item, persist it, create a new LineItem, adding it to Item and persist it and flush all the Doctrine operations, when I try to get the lineItems ($item->getLineItems()) I get no elements, but in MySQL all the data is correctly stored. I can also see, in the debug log, that the data was inserted (there are two INSERT operations), but there isn’t any SELECT. Does that mean that I have to reload the entity before accessing the child entities from the main entity?

The code of the controller (it’s a minimal test):

 * @Route("/test", name="test")
public function index(): Response
    $em = $this->getDoctrine()->getManager();

    $item = new Item();
    $item->setName('Just an item');


    $lineItem = new LineItem();
    $lineItem->setName('a line item');


    $lines = [];
    foreach ($item->getLineItems() as $line) {
        $lines[] = [
            'name' => $line->getName(),
            'quantity' => $line->getQuantity()


    return new Response('All correct');

The result of the die(var_dump($lines)) command it’s just array(0) { }.

If all I need is to reload the $item variable, there is any method to do this, or I need to call the repository ($em->getRepository('App:Item')->find($item->getId())).

Thank you!

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *