0

I have following entities:

Application:

/**
 * Care worker
 *
 * @var CareWorker
 * @ORM\ManyToOne(targetEntity="Sme\CareWorkerBundle\Entity\CareWorker", inversedBy="application", cascade={"persist"})
 * @ORM\JoinColumn(referencedColumnName="id")
 *
 */
private $careWorker;

And Careworker:

/**
 * Application
 *
 * @var Application[]
 * @ORM\OneToMany(targetEntity="Sme\CareWorkerBundle\Entity\Application\Application", mappedBy="careWorker", cascade={"persist", "remove"})
 *
 */
private $application;

I am trying to retrieve the careworker field from the application using this query:

 $qb=$em->createQueryBuilder()
        ->select('a.careWorker')
        ->from('SmeCareWorkerBundle:Application\Application','a')
    ;
    $query=$qb->getQuery();

    $query->getResult();

I am getting this query exception

[Semantical Error] line 0, col 9 near 'careWorker FROM': Error: Invalid PathExpression. Must be a StateFieldPathExpression.

Can you please tell me how to solve this?

Adam
  • 3,415
  • 4
  • 30
  • 49
  • 2
    Possible duplicate of [Symfony2 and Doctrine - Error: Invalid PathExpression. Must be a StateFieldPathExpression](http://stackoverflow.com/questions/14216470/symfony2-and-doctrine-error-invalid-pathexpression-must-be-a-statefieldpathe) – scoolnico Jan 08 '16 at 10:17
  • Are you trying to get a list of care worker entities? You will need to select from careworker instead of application. If you need to filter by application then join it and apply your where clauses. – Cerad Jan 08 '16 at 12:58
  • @Cerad I need to get the list of Applications, so I can join it later with Careworkers, so then I can join it further. – Adam Jan 11 '16 at 09:53

2 Answers2

0

try this:

$qb=$em->createQueryBuilder()
        ->select('a.careWorker')
        ->from(Sme\CareWorkerBundle\Entity\Application\Application::class,'a')
    ;
    $query=$qb->getQuery();

    $query->getResult();

be careful to first arg of "from" functio

M Gholami
  • 951
  • 1
  • 13
  • 32
0

To get all applications and its corresponding careWorker:

$qb = $em->createQueryBuilder()
    ->select('a')
    ->addSelect('cw')
    ->from('SmeCareWorkerBundle:Application\Application', 'a')
    ->leftJoin('a.careWorker', 'cw')
;

$result = $qb->getQuery()->getResult();
Yassine Guedidi
  • 1,695
  • 11
  • 12
  • Between Careworker and Application I have one to many relation. I need to get all applications first and then all corresponding CareWorker. – Adam Jan 11 '16 at 09:56