symfony doctrine验证实体关系错误

问题描述 投票:0回答:1

我有两个实体用户和设备具有一对多双向关系。

问题是在我运行此命令时

php bin/console doctrine:schema:validate

我明白了

* The association App\Entity\User#devices refers to the owning side field App\Entity\Device#user which is not defined as the association, but as the field.

* The association App\Entity\User#devices refers to the owning side field App\Entity\Device#user which does not exist.

用户实体

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

/**
 * @ORM\OneToMany(targetEntity="Device", mappedBy="user")
 */
private $devices;

设备实体

/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 */
private $id;


/**
 * @ORM\Column(type="integer",name="user_id")
 * @ORM\ManyToOne(targetEntity="User", inversedBy="devices")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=true)
 */
private $user;
php symfony doctrine-orm
1个回答
2
投票

是的,您必须在数据库表中将用户定义为association not as a field

您的设备实体应该像:

/**
 * @ORM\ManyToOne(targetEntity="User", inversedBy="devices")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=true)
 */
private $user;

名为user的字段不应该在您的表中,它会自动生成一个名为user_id的字段。您无需手动添加它。

有关更多信息,请参阅Symfony Doctrine associations

© www.soinside.com 2019 - 2024. All rights reserved.