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?
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 }}
If you can't use a querybuilder you could force Doctrine to always load the owner on the product whenever the product is loaded by EAGER loading the association:
/**
* @ORM\OneToOne(targetEntity="Application\Entity\Owner", fetch="EAGER")
*/
protected $owner;
http://doctrine-orm.readthedocs.org/en/latest/reference/working-with-objects.html#by-eager-loading
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.