【问题标题】:Semantical Error line 0, col 63 near g, product p: Error: Class AppBundle\Entity\Product has no association symfony2 application语义错误 line 0, col 63 near g, product p: Error: Class AppBundle\Entity\Product has no association symfony2 application
【发布时间】:2016-05-23 21:38:09
【问题描述】:

我想以一对多的关系访问实体的曾孙。例如:部门实体(父)-> 类别实体(子)-> 集团实体(孙)-> 产品实体(曾孙)。我想根据他们的部门获取产品。

与类别的部门关系:

/**
 *  @ORM\OneToMany(targetEntity="Category", mappedBy="department")
 */
protected $category;
public function __construct()
{
    $this->category = new ArrayCollection();
}

与部门和组的类别关系:

 /**
 * @ORM\ManyToOne(targetEntity="Department", inversedBy="category")
 * @ORM\JoinColumn(name="department_id", referencedColumnName="id")
 */
protected $department;
/**
 *  @ORM\OneToMany(targetEntity="Group", mappedBy="category")
 */
protected $group;
public function __construct()
{
    $this->group = new ArrayCollection();
}

与类别和产品的组关系:

/**
 * @ORM\ManyToOne(targetEntity="Category", inversedBy="group")
 * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
 */
protected $category;

/**
 *  @ORM\OneToMany(targetEntity="Product", mappedBy="group")
 */
protected $product;
public function  construct()
{
    $this->product = new ArrayCollection();
}

/**
 * @ORM\ManyToMany(targetEntity="Brand", inversedBy="group")
 * @ORM\JoinTable(name="brand_groups")
 */
private $brand;

public function __construct()
{
    $this->brand = new ArrayCollection();
}

与集团的产品关系:

/**
 * @ORM\ManyToOne(targetEntity="Group", inversedBy="product")
 * @ORM\JoinColumn(name="groups_id", referencedColumnName="id")
 */
protected $group;

ProductRepository:

namespace AppBundle\Repository;

use Doctrine\ORM\EntityRepository;
use AppBundle\Entity\Product;

/**
 * ProductRepository
 */
class ProductRepository extends EntityRepository
{
    public function findDepartmentProduct($department)
    {
        return $this
            ->createQueryBuilder('p')
            ->select(array('p', 'g','c','d'))
            ->from('Product', 'p')
            ->join('p.Group', 'g')
            ->join('g.Category', 'c')
            ->join('c.Department', 'd')
            ->where('d = :department')
            ->setParameter('department', $department)
            ->getQuery();
    }
}

默认控制器:

/**
 * @Route("/department/{id}", name = "department")
 */
public function departmentAction(Request $request,$id)
{
    $Department = $this->getDoctrine()->getRepository('AppBundle:Department');
    $department = $Department->find($id);

    $Product = $this->getDoctrine()->getRepository('AppBundle:Product');
    $customersChoiceProducts = $Product->mostView('20');

    $query = $query = $Product->findDepartmentProduct($department);

    $paginator  = $this->get('knp_paginator');
    $pagination = $paginator->paginate(
        $query, 
        $request->query->getInt('page', 1),
        8/*limit per page*/
    );

    return $this->render('default/department.html.twig', array(
        'pagination' => $pagination,
        'department' => $department,
        'customersChoiceProducts' => $customersChoiceProducts,
    ));
}

然后我得到这个错误:

[Semantical Error] line 0, col 63 near 'g, product p': Error: Class AppBundle\Entity\Product has no association named groups

请帮帮我。

【问题讨论】:

  • 你加入的是关联,而不是实体类名,所以你应该有->join('p.group', 'g')而不是->join('p.Group', 'g')
  • 谢谢@Jason 正在使用 knp 分页器,现在我收到此错误无法计数选择两个 FROM 组件的查询,无法区分。 ->createQueryBuilder('p') ->select('p') ->from('AppBundle:Product','l') ->join('p.group', 'g') ->join('g .category', 'c') ->join('c.department', 'd') ->where('d = :department') ->setParameter('department', $department) ->getQuery();
  • 试试->createQueryBuilder('p')->join('p.group', 'g') ->join('g.category', 'c') ->join('c.department', 'd') ->where('d = :department') ->setParameter('department', $department) ->getQuery();
  • 谢谢你,我通过添加 ->getResult() 修复了它。所以我没有将查询传递给 knp 分页器,而是传递了结果

标签: php mysql symfony doctrine-orm


【解决方案1】:

我犯的错误是加入实体类名而不是关联。我的新代码是:

public function findDepartmentProduct($department)
{
    return $this
        ->createQueryBuilder('l')
        ->select('p')
        ->from('AppBundle:Product','p')
        ->join('p.group', 'g')
        ->join('g.category', 'c')
        ->join('c.department', 'd')
        ->where('d = :department')
        ->setParameter('department', $department)
        ->getQuery();
}

如果你运行它,你会得到以下错误:

无法统计选择两个FROM组件的查询,无法区分

你可以通过添加->getResult()来解决这个错误

public function findDepartmentProduct($department)
{
    return $this
        ->createQueryBuilder('l')
        ->select('p')
        ->from('AppBundle:Product','p')
        ->join('p.group', 'g')
        ->join('g.category', 'c')
        ->join('c.department', 'd')
        ->where('d = :department')
        ->setParameter('department', $department)
        ->getQuery()
        ->getResult();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 2019-06-13
    • 1970-01-01
    • 2017-07-18
    • 2017-11-07
    • 2020-11-06
    • 1970-01-01
    相关资源
    最近更新 更多