Symfony:连接2个表以在单独的列中获取值时出错

时间:2016-05-13 15:21:01

标签: php symfony doctrine-orm doctrine

我想从表名'sales'获取3个值,2从名为'products'的表中获取1,我在DQL中写道:

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

但是我收到了这个错误:

  

[语义错误]第0行,第81行靠近'SystemBundle:Product':错误:类'SystemBundle \ Entity \ Product'未定义。

这是我第一次使用学说中的连接,所以我想我在这里搞乱语法..请帮助解决这个问题

谢谢

1 个答案:

答案 0 :(得分:1)

使用doctrine方法,您可以获取所需的结果:

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