Question

I have this entity:

class TruckOrderPud {
/**
 * @ORM\Id     
 * @ORM\OneToOne(targetEntity="Pud")
 * @ORM\JoinColumn(name="pud_id", referencedColumnName="id", onDelete="CASCADE")
 */
protected $pudId;

/**
 * @ORM\Id
 * @ORM\OneToOne(targetEntity="TruckOrder")
 * @ORM\JoinColumn(name="truck_order_id", referencedColumnName="id", onDelete="CASCADE")
 */
protected $truckOrderId;

/**
 * @ORM\Column(name="package_count", type="integer", options={"unsigned"=true}, nullable=true)
 */
protected $packageCount;

/**
 * @ORM\Column(name="package_weight", type="integer", options={"unsigned"=true}, nullable=true)
 */
protected $packageWeight;

}

I am needing get only single column pudId from this entity by truckOrderId parameter.

I am try:

   $result = $this->_em->createQuery('SELECT top.pudId FROM AppTruckingBundle:TruckOrderPud top WHERE top.truckOrderId = ?1')
                    ->setParameter(1, $truckOrderId)
                    ->getSQL();`

Then I have error:

"[Semantical Error] line 0, col 11 near 'pudId FROM AppTruckingBundle:TruckOrderPud': Error: Invalid PathExpression. Must be a StateFieldPathExpression."

If i change query from 'SELECT top.pudId...' on 'SELECT top...' is it all OK.

I am also try:

    $qb = $this->_em->createQueryBuilder();

    $qb->select('top.pudId')
                 ->from('AppTruckingBundle:TruckOrderPud', 'top')
                 ->where($qb->expr()->eq('top.truckOrderId', $truckOrderId));`

In this case I am getting identical error. If i change $qb->select('top.pudId') on $qb->select('top') is it all OK.

Thanks for help!

Was it helpful?

Solution

PudId is a foreign key that refers to Pud, so you have to refer to Pud. Try this

$qb->select('IDENTITY(top.pud) PudId')
->from('AppTruckingBundle:TruckOrderPud', 'top')
->where($qb->expr()->eq('top.truckOrderId', $truckOrderId));`

OTHER TIPS

Try to remove the 1 from the ?1 and leave only ?:

   $result = $this->_em->createQuery('SELECT top.pudId FROM AppTruckingBundle:TruckOrderPud top WHERE top.truckOrderId = ?')
                    ->setParameter(1, $truckOrderId)
                    ->getSQL();

Doctrine does the parameter positioning by itself (first param on the first question mark spot). Read here for details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top