首页 > 解决方案 > 我如何使用与 Doctrine 的关系?

问题描述

我正在一个网站上存储系列。

我的数据库包含一个 Programs 表和一个 Category 表。类别表有一个ID和一个name

Programs 表包含有关该程序的所有信息以及与其关联的类别 ID。

我有一个/category/$categoryName功能。

我想通过user-specified $categoryName,显示此类别中的所有程序。

public function category(string $categoryName):Response{
    $categoryInfos = $this->getDoctrine()
        ->getRepository(Category::class)
        ->findOneByName($categoryName);

    $categoryID = $categoryInfos['id'];

给我->

“不能将 App\Entity\Category 类型的对象用作数组”

拥有 后category ID,我可以显示属于该类别的所有程序,但

1)我无法检索身份证

2)我想知道是否没有更简单的命令来做我想做的事情。

标签: phpmysqlsymfonydoctrine-orm

解决方案


/**
 * @Route("/category/{categoryName}", name="category")
 */
public function category(string $categoryName): Response
{
    $categoryInfos = $this->getDoctrine()
        ->getRepository(Category::class)
        ->findOneByName($categoryName);

    $categoryInfos->getId();

    return $this->render('home.html.twig');
}

嗯,就是这样。


推荐阅读