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...)

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...

 {% 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. This array works when sent to the twig template

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

Thanks!

Answers


You need to request pr in SELECT part ;)

$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;
    }

Need Your Help

How to remove all folders named "Sample" from directory and subfolders?

batch-file rmdir delete-directory

I'm looking for a command line that I can add to a batch that I use.

Problems updating MySQL, "username" in a table using PHP

php mysql sql-update

I'm probably not using the best method to create a user system, but it doesn't need to be fancy. I also know that I'm not the most organized