我遇到OneToMany关系的问题,我有用户可以献血,所以他可以献血,我的应用程序工作正常,但在我的探查器中我有2个错误。
来自用户实体:
The association AppBundle\Entity\Users#userDonation refers to the owning side field AppBundle\Entity\UserDonates#id which is not defined as association, but as field.
The association AppBundle\Entity\Users#userDonation refers to the owning side field AppBundle\Entity\UserDonates#id which does not exist.
来自UserDonates:
The mappings AppBundle\Entity\UserDonates#userId and AppBundle\Entity\Users#userDonation are inconsistent with each other.
以下是我的实体:
UserDonates:
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="Users", inversedBy="userDonation")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $userId;
/**
* @var string
*
* @ORM\Column(name="place", type="string", length=255)
*/
private $place;
/**
* @var date
* @ORM\Column(name="donation_date", type="date")
*/
private $donation_date;
/**
* @var string
* @ORM\Column(name="donation_type", type="string", length=255)
*/
private $donation_type;
/**
* @var integer
* @ORM\Column(name="blod_donated", type="integer")
*/
private $blood_donated;
用户:
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\OneToMany(targetEntity="UserDonates", mappedBy="id", cascade={"persist","remove"})
*/
protected $userDonation;
/**
* @ORM\OneToOne(targetEntity="UserInfo", cascade={"persist","remove"})
*/
private $profil;
//__construct() from FOSUserBundle
public function __construct(){
parent::__construct();
}
用户实体也与具有OneToOne关系的UserInfo相关。
答案 0 :(得分:3)
我在这里看到两个问题。
关联AppBundle \ Entity \ Users#userDonation指的是拥有方字段AppBundle \ Entity \ UserDonates #id,它不是定义为关联,而是作为字段。
User::$userDonation
关联的反面不是{{1}}字段,而是id
实体中的userId
字段。映射应如下所示:
UserDonation
作为旁注,我建议改为命名/**
* Here! -----------------------------------------------v
* @ORM\OneToMany(targetEntity="UserDonates", mappedBy="userId", cascade={"persist","remove"})
*/
protected $userDonation;
属性userId
;毕竟它将包含一个实际的用户对象,而不仅仅是一个用户的ID。
关联AppBundle \ Entity \ Users#userDonation指的是不存在的拥有方字段AppBundle \ Entity \ UserDonates#id。
您的user
属性是私有的。由Doctrine管理的所有属性都必须为UserDonates::$id
,以便Doctrine能够使用数据填充它们。