我的OneToMany关系有问题,我有一个可以献血的用户,所以他可以进行多次献血,我的应用程序运行良好,但在我的探查器中我有两个错误。
来自用户实体:
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.
以下是我的实体:
用户捐赠:
/**
* @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();
}
Users实体也与具有OneToOne关系的UserInfo相关。
我在这里看到了两个问题。
关联AppBundle''Entity''Users#userDonate是指拥有方字段AppBundle''Pentity''UserDonates#id,该字段未定义为关联,而是定义为字段。
User::$userDonation
关联的反面不是id
字段,而是UserDonation
实体中的userId
字段。映射应该是这样的:
/**
* Here! -----------------------------------------------v
* @ORM'OneToMany(targetEntity="UserDonates", mappedBy="userId", cascade={"persist","remove"})
*/
protected $userDonation;
附带说明一下,我建议将userId
属性命名为user
;毕竟,它将包含一个实际的用户对象,而不仅仅是用户的ID。
关联AppBundle''Entity''Users#userDonate指的是不存在的拥有方字段AppBundle''Pentity''UserDonates#id。
您的UserDonates::$id
属性是私有的。条令管理的所有财产都需要是protected
,以便条令能够用数据填充它们。