2016-05-13 48 views
1

我想從一個名爲「產品」表取3個值,2從表名「銷售」和1來獲取值,我寫了下面的DQL:Symfony的:錯誤而加入2個表中單獨列

$repo = $em->getRepository('SystemBundle:Sales'); 
     $q = $repo->createQueryBuilder('s') 
      ->select('s.units','s.timestamp','p.price') 
      ->join('SystemBundle:Product', 'p') 
      ->where('s.prodId = p.id AND p.company=:comp') 
      ->setParameter('comp', $cid) 
      ->getQuery() 
      ->getArrayResult(); 
     return new JsonResponse($q); 

,但我收到此錯誤:

[Semantical Error] line 0, col 81 near 'SystemBundle:Product': Error: Class 'SystemBundle\Entity\Product' is not defined.

是我第一次在教義加入工作,所以我想我有語法搞亂這裏..請幫助解決這個

感謝

+0

不銷售有關係的產品?如果是的話,連接應該是' - >連接('s.products','p');'其中「產品」是您設置爲關係名稱的名稱。並且prodId = id應該跳過的部分 – JimL

+0

爲什麼你沒有使用原則獲取結果? –

回答

1

教條的方法,你可以獲取你所需要的結果:

$productRepository = $em->getRepository('SystemBundle:Product'); 
$oProduct = $productRepository->findBy(array('company' => $cid)); 

$oCompany = $oProduct->getCompany(); 

$result = array(
    'units' => $oCompany->getUnits(), 
    'timestamp' => $oCompany->getTimestamp(), 
    'price' => $oProduct->getPrice(), 
); 

$response = new JsonResponse(); 
$response->setData($result); 

return $response;