我在学说2中遇到了以下问题,我想将我的地址实体和国家/地区实体加入我的用户实体。所以经过一些尝试,它在数据库中工作正常,但当我使用./vendor/bin/doctrine-module orm:validate-schema
时
它告诉我一些映射失败。
[Mapping] FAIL - The entity-class 'User\Entity\Adress' mapping is invalid:
* The association User\Entity\Adress#user refers to the inverse side field User\Entity\User#use
r_id which does not exist.
* The association User\Entity\Adress#country refers to the inverse side field User\Entity\Count
ry#id which is not defined as association.
* The association User\Entity\Adress#country refers to the inverse side field User\Entity\Count
ry#id which does not exist.
我真的不知道为什么会这样,因为我的数据库工作正常。它识别地址中的用户和国家/地区实体以及值。
我的用户实体:
...
/**
* Entity Class representing a post of our User module.
*
* @ORM\Table(name="user")
* @ORM\Entity(repositoryClass="User\Repository\UserRepository")
*
* @property int userid
* @property string firstname
* @property string insertion
* @property string lastname
* @property date dateofbirth
* @property string gender
* @property string phonenumber
* @property string mobile
*/
class User extends zfcUser implements UserInterface
{
/**
* Id from user
*
* @ORM\OneToMany(targetEntity="Adress", mappedBy="user", cascade= "remove")
* @access protected
*/
/**
* Firstname of our user
*
* @ORM\Column(type="string", nullable=true)
* @var string
* @access protected
*/
protected $firstname;
...
...
我的地址实体:
/**
* Entity Class representing our Adress module.
*
* @ORM\Entity
* @ORM\Table(name="adress")
* @property integer addressid
* @property string street
* @property integer number
* @property string addition
* @property string zipcode
* @property integer userid
* @property integer countryid
*/
class Adress
{
...
...
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="user_id")
* @ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
* @var User[]
* @access protected
*/
protected $user;
/**
* @ORM\ManyToOne(targetEntity="Country", inversedBy="id")
* @ORM\JoinColumn(name="country_id", referencedColumnName="id")
* @var Country[]
* @access protected
*/
protected $country;
...
...
我的国家实体:
...
...
/**
* Entity Class representing our Country module.
*
* @ORM\Entity
* @ORM\Table(name="country")
* @property integer id
* @property string countrycode
* @property string countryname
*/
class Country
{
/**
* Id from a country
*
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\OneToMany(targetEntity="Adress", mappedBy="country")
* @var int
* @access protected
*/
protected $id;
...
...
有人解释为什么会这样?
答案 0 :(得分:4)
第一个错误:
在您的User
班级中缺少与地址相关的属性,因此可能有两种可能:
protected $user_id
:
这不起作用,因为Doctrine期望在这个地方有一组地址对象。如果是protected $addresses
:
您必须将用户属性的Address
类中的映射更改为:
@ORM\ManyToOne(targetEntity="User", inversedBy="addresses")
对于第二个错误:
在Address
课程中,您必须删除国家/地区的inversedBy属性:
@ORM\ManyToOne(targetEntity="Country")