symfony 无法以多对多关系连接相关表

symfony cannt join related tables in manytomany relationship

我无法在多对多关系中连接表(ProductsCategory)。

My Entities

我在尝试加入他们时收到此错误消息

[Syntax Error] line 0, col 511: Error: Expected Literal, got 'JOIN' 

我的存储库

public function GetProductsList($productid){
$fields = array['product.product','category.category']
$query = $this->getEntityManager()->createQueryBuilder()
        ->select($fields)
        ->from('TestMyBundle:product','product')
        ->innerJoin('TestMyBundle:category','category')
        ->where('product.productid=:productid')
        ->setParameter('productid',$productid)
        ->getQuery()
        ->getResult();
return $query;

}

您的联接看起来有误,我不明白您为什么需要硬编码数组。也可以将实体别名直接放在 select 中。我会写成:

public function GetProductsList($productid){

    $result = $this->getEntityManager()->createQueryBuilder()
        ->select('product', 'category')
        ->from('TestMyBundle:product','product')
        ->join('product.categories','category') // making an assumption your product entity has getCategories()
        ->where('product.productid=:productid')
        ->setParameter('productid',$productid)
        ->getQuery()
        ->getResult();

    return $result;
}

应该可以了。

请注意,当您遇到此类问题时,使用 ->getSQL() 而不是 getQuery() 和 var_dump SQL 来查看querybuilder 正在生成,使调试更容易。