简体   繁体   English

访问主义查询中的元素不起作用

[英]Accessing Elements In Doctrine Query Not Working

I have a Doctrine query, where the "JOIN p.product pr" is joining in the Product Table and all of its info (id, name, qty, status, created, updated, deletedAt, etc...) 我有一个Doctrine查询,其中“ JOIN p.product pr”正在加入产品表及其所有信息(id,名称,数量,状态,创建,更新,deletedAt等)。

Custom function in my repository class... 我的存储库类中的自定义函数...

 $query = $this->getEntityManager()
        ->createQuery('
        SELECT      p
        FROM        WIC\PurchaseOrderBundle\Entity\PurchaseOrderProductsStatus p
        JOIN        p.product pr
        WHERE       p.inventoryLocation = :id
        AND         p.account = :account_id')
        ->setParameter('id', $id)
        ->setParameter('account_id', $account_id);

    try{
        return $query->getArrayResult();
    }catch (\Doctrine\ORM\NoResultException $e) {
        return $e;
    }

In my controller I am sending the array into a twig template... 在我的控制器中,我将数组发送到树枝模板中。

 $productActions = My custom function query listed above which returns array

 return array(
        'heading' => 'Inventory Staging Location',
        'productActions' => $productActions,
    );

In my Twig template I am now trying to loop through the records... 在我的Twig模板中,我现在尝试遍历记录...

 {% for productAction in productActions %}
                        <tr>
                            <td>{{ productAction.product.sku }}</td>
                            <td>{{ productAction.product.name }}</td>
                            <td></td>
                        </tr>
                    {% endfor %}

I am getting this error... 我收到此错误...

 Key "product" for array with keys "id, qty, status, created, updated, deletedAt" does not exist in ...

What Am I doing wrong? 我究竟做错了什么?

When I do my query by doing a ->findBy() everything seems to work fine. 当我通过-> findBy()进行查询时,一切似乎都正常运行。 This array works when sent to the twig template 发送到树枝模板时,此数组有效

 $productActions = $em->getRepository('WICPurchaseOrderBundle:PurchaseOrderProductsStatus')->findBy(array(
            "inventoryLocation"=>$inventoryLocation,
            "account"=>$account_id,
        )); 

Thanks! 谢谢!

You need to request pr in SELECT part ;) 您需要在SELECT部分​​中请求pr ;)

$query = $this->getEntityManager()
        ->createQuery('
        SELECT      p, pr
        FROM        WIC\PurchaseOrderBundle\Entity\PurchaseOrderProductsStatus p
        JOIN        p.product pr
        WHERE       p.inventoryLocation = :id
        AND         p.account = :account_id')
        ->setParameter('id', $id)
        ->setParameter('account_id', $account_id);

    try{
        return $query->getArrayResult();
    }catch (\Doctrine\ORM\NoResultException $e) {
        return $e;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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