symfony - Symfony3 Doctrine : Fetch entities as arrays with all associations for performance improvement -
i have 2 entities : product , tag. product can has 0 or 1 tag.
product entity :
/** * @orm\column(type="string", length=25) * @orm\id */ private $code; /** * @orm\column(type="string", length=50) */ private $name; /** * @orm\column(type="string", length=35, nullable=true) */ private $tagcode; /** * @orm\manytoone(targetentity="tag") * @orm\joincolumn(name="tagcode", referencedcolumnname="tagcode") */ private $tag;
i have page want list products. use knppaginator bundle paginate 1000+ results (50 per page).
productcontroller listaction()
$em = $this->getdoctrine()->getmanager(); $query = $em->createquery('select p appbundle:product p order p.code asc'); // pagination $paginator = $this->get('knp_paginator'); $pagination = $paginator->paginate($query, $request->query->getint('page', 1), 50); return $this->render('products/list.html.twig', array( 'products' => $pagination ));
it works it's little bit time consuming (more 7 seconds display 50 first results).
instead of fetching products entities (my entities quite large lot of dependencies , associations), use arrays (for increasing page loading speed).
besides, want show properties of entity product.
- product name
- product code (= primary key)
- tag code (= secondary key)
- tag name
i tried use $query->getarrayresult()
doesn't return tag
object , have tag
object show tag name in twig :
{% product in products %} ... <td>{{ product.tag.name }}</td> ... {% endfor %}
$query->getarrayresult()
returns array of products :
array (size=3) 'code' => string 'test' (length=4) 'name' => string 'my product' (length=10) 'tagcode' => string 'mytag' (length=5)
my question : how tag
object (or tag name) in array of results ?
Comments
Post a Comment