Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch extra data to feed a symfony2 collection field type?

I am using a basic collection field with Symfony2, and everything is working well.

Say I have:

->add('product', 'collection', array(...))

Now, in my view, I am calling product.vars.data.owner where I have a oneToOne product->getOwner().

This is generating one extra request per product in the collection. Since I can't use a querybuilder in a collection field, how can I make sure it doctrine fetches the product owners in order to avoid those many extra requests?

like image 372
Sébastien Avatar asked Dec 11 '25 01:12

Sébastien


1 Answers

This is an example(I don't see your data mapping & form builder), I hope this help you :

1 : Fetch your data by dql,

$dql = "select c from category c left join c.product p join p.owner o where c.id = ?1";
$category = $this->createQuery($dql)->setParameter(1, $id)->getSingleResult();

2 : Suppose you have a category form that could have many products:

class Category
{
    // manyToMany
    private $product
}

$form = $this->createForm(new CategoryType(), $category);

3 : If your collection type be an embedded ProductType(form type) then you can use below line in your template:

{{ form.product.vars.data.owner }} 
like image 157
ghanbari Avatar answered Dec 12 '25 13:12

ghanbari



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!