symfony doctrine 2 OneToMany - 与反面字段错误的关联

symfony doctrine 2 OneToMany - association to the inverse side field error

我想连接两个实体

某些菜品类别中的菜品

菜肴 (category_id) 与菜肴类别 (id)

出现错误:

关联AppBundle\Entity\Dish#categoryId引用了不存在的反向字段AppBundle\Entity\DishCategory#category_id。

这些是我的实体类

盘子实体

class Dish
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
     private $id;

    /**
     *@ORM\ManyToOne(targetEntity = "DishCategory",inversedBy="category_id",cascade={"persist"})
     * @ORM\JoinColumn(name="id",referencedColumnName="id")
     */
     private $categoryId;
}

DishCategory 实体


class DishCategory
{

    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\OneToMany(targetEntity="Dish", mappedBy="category_id")
     * @ORM\JoinColumn(name="category_id",referencedColumnName="id")
     */
     private $id;
}

在 DishController 中我 运行 这个函数到存储库


$disishes = $em->getRepository('AppBundle:Dish')->findAllAsArray();

这就是 DishRepository 的样子

public function findAllAsArray()
{
    return $q = $this->createQueryBuilder('d')
        ->join('d.categoryId','c')
        ->select('d.id as id_dish','c.id as id_cat','d.price')
        ->orderBy('c.position', 'asc')
        ->orderBy('d.position', 'asc')
        ->getQuery()
        ->getResult(Query::HYDRATE_ARRAY);
    }

我已经阅读了很多关于 OneToMany 的教程,但我仍然找不到问题所在:(

仍然出现错误:

The association AppBundle\Entity\Dish#categoryId refers to the inverse side field AppBundle\Entity\DishCategory#category_id which does not exist.

:(

我猜category_id是外键。 Don't map foreign keys to fields in an entit 因为:

Foreign keys have no meaning whatsoever in an object model. Foreign keys are how a relational database establishes relationships. Your object model establishes relationships through object references. Thus mapping foreign keys to object fields heavily leaks details of the relational model into the object model, something you really should not do.

您的 DishCategory 实体是拥有方,因为它持有外键。顺便更新你的代码如下:

盘实体

class Dish
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
     private $id;


    /**
     * @ORM\ManyToOne(targetEntity="DishCategory", inversedBy="dishes")
     * @ORM\JoinColumn(name="dish_category_id",referencedColumnName="id")
     */
     private $dishCategory;
}

DishCategory 实体

class DishCategory
{

    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
     private $id;

     /**
      * @ORM\OneToMany(targetEntity="Dish", mappedBy="dishCategory")
      */
      private $dishes;

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

然后在你的 DishRepository

public function findAllAsArray()
{
    return $q = $this->createQueryBuilder('d')
        ->join('d.category','c')
        ->select('d.id as id_dish','c.id as id_cat','d.price')
        ->addOrderBy('c.position', 'asc')
        ->addOrderBy('d.position', 'asc')
        ->getQuery()
        ->getResult(Query::HYDRATE_ARRAY)
    ;
}

但我看不出执行双重 orderBy 的意义,因为 multiple calls to orderBy do not stack,如果要实现此目的,请使用 addOrderBy。而且 Query::HYDRATE_ARRAY 也不是必需的,我猜,因为由于自定义查询,您将拥有一个数组,而不是实体。