简体   繁体   中英

Symfony2 - Custom Query in Entity Repo doesn't seem to return expected results

So I came to a point where I needed to create a custom query in my Entity Repository to do a sub-select.

The query itself seems sound and does indeed find the result I am after (I checked by running the query in Navicat) however, when trying to view the results (using Twig templates) I am getting the following error:

Item "id" for "Array" does not exist in DEMODemoBundle:Staff/Company:company.html.twig at line 42

Line 42 in that template is:

<td><a href="{{ path('staff_company_show', { 'id': entity.id }) }}">{{ entity.id }}</a></td>

Previously, I was using a basic "findAll" query in my controller, and the Twig template worked fine (it uses a for loop to go through the results and print out a table)

So, I was under the assumption that if my custom query fetched a list of results, with all the same columns (just 1 added extra in the sub-select, not the ID mentioned though) then everything should be fine!

But clearly not. It seems as though there is another Array level for some reason and I am unsure as to why?

Here is my custom query in the repo (it does a sub select to create a parent_name column):

public function getCompanyWithParent()
{
    $dql = 'SELECT c, (SELECT p.name FROM DEMO\DemoBundle\Entity\User\Company p WHERE p.id = c.parent) as parent_name FROM DEMO\DemoBundle\Entity\User\Company c';

    $query = $this->getEntityManager()->createQuery($dql);

    return $query->getArrayResult();
}

I have tried both:

return $query->getArrayResult();

and

return $query->getResult();

But to no avail.

Any ideas folks?

Sorted it. Seems I had to specify the columns I wanted to pull through eg SELECT c.id, c.name ..., and sadly couldn't just use SELECT c, ... FROM ...

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM