【问题标题】:Symfony 5 - Semantical *** Error has no field or association named category.idSymfony 5 - 语义 *** 错误没有名为 category.id 的字段或关联
【发布时间】:2020-12-13 17:04:28
【问题描述】:

我有这个问题,我真的不知道为什么......

存储库:

    /**
     * @return Creations[]
     */

    public function displayOne(): array
    {
        return $this->createQueryBuilder('c')
            ->andWhere('c.category.id = 1')
            ->setMaxResults(3)
            ->getQuery()
            ->getResult();
        // returns an array of Product objects
        return $query->getResult();
    }


}

实体:

    /**
     * @ORM\ManyToOne(targetEntity=Category::class, inversedBy="creations")
     * @ORM\JoinColumn(nullable=false)
     */
    private $category;

控制器功能:

    public function index(): Response
    {

        $repository = $this->entityManager->getRepository(Creations::class);
        $creations = $repository->findAll();


        $categories = $this->entityManager->getRepository(Category::class)->findAll();

        return $this->render('creations/index.html.twig', [

            'creations' => $repository->displayOne(),
            'categories' => $categories

        ]);
    }

另外,当我 dd($creations) 时,我得到了很好的“链接”thecreation -> category -> id -> 1 例如

感谢您的帮助!

【问题讨论】:

    标签: php doctrine-orm doctrine symfony4 symfony5


    【解决方案1】:

    试试这个:

    public function displayOne(Category $category): array
    {
        return $this->createQueryBuilder('c')
            ->andWhere('c.category = :category')
            ->setParameter('category', $category)
            ->setMaxResults(3)
            ->getQuery()
            ->getResult();
    }
    

    或者:

    public function displayOne(): array
    {
        return $this->createQueryBuilder('c')
            ->join('c.category', 'cat')
            ->andWhere('cat.id = :category')
            ->setParameter('category', 1)
            ->setMaxResults(3)
            ->getQuery()
            ->getResult();
    }
    

    三部分选择器c.category.id 是行不通的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-15
      • 1970-01-01
      • 2014-05-31
      • 1970-01-01
      • 2013-04-24
      • 1970-01-01
      相关资源
      最近更新 更多