Orm 与条令映射条令2关联失败
我对原则2有以下问题,我想将我的地址实体和国家实体加入到我的用户实体中。因此,经过一些尝试后,它在数据库中运行良好,但是当我使用Orm 与条令映射条令2关联失败,orm,doctrine-orm,mapping,associations,Orm,Doctrine Orm,Mapping,Associations,我对原则2有以下问题,我想将我的地址实体和国家实体加入到我的用户实体中。因此,经过一些尝试后,它在数据库中运行良好,但是当我使用/vendor/bin/doctor模块orm:validate schema 它向我显示了一些映射失败 [Mapping] FAIL - The entity-class 'User\Entity\Adress' mapping is invalid: * The association User\Entity\Adress#user refers to the i
/vendor/bin/doctor模块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;
...
...
有人解释了为什么会发生这种情况吗?对于第一个错误:
在User
类中,与地址相关的属性丢失,因此可能有两种可能性:
受保护的$user\u id
:
这不起作用,因为条令要求在这个位置有一个地址对象数组受保护的$addresses
:
您必须将用户属性上的Address
类中的映射更改为:
@ORM\manytone(targetEntity=“User”,inversedBy=“addresses”)
Address
类中,您必须删除country上的inversedBy属性:
@ORM\ManyToOne(targetEntity="Country")
对于第一个错误:
在User
类中,与地址相关的属性丢失,因此可能有两种可能性:
受保护的$user\u id
:
这不起作用,因为条令要求在这个位置有一个地址对象数组受保护的$addresses
:
您必须将用户属性上的Address
类中的映射更改为:
@ORM\manytone(targetEntity=“User”,inversedBy=“addresses”)
Address
类中,您必须删除country上的inversedBy属性:
@ORM\ManyToOne(targetEntity="Country")
好了,第二个问题解决了。但我的第一个错误不是。我的userclass中有一个受保护的$user\u id。。但是它是由zfcUser(其中设置了$user\u id)扩展的。我希望在我的地址中包含用户id(因为一个用户可以有多个地址)。如何修复该错误?只需调用
User
class$address中的OneToMany属性,您也需要$User\u id,但当它位于zfcUser中且该类是映射超类时,它应该可以工作。Oke,第二个问题已解决。但我的第一个错误不是。我的userclass中有一个受保护的$user\u id。。但是它是由zfcUser(其中设置了$user\u id)扩展的。我希望在我的地址中包含用户id(因为一个用户可以有多个地址)。如何修复该错误?只需调用User
class$address中的OneToMany属性,您也需要$User\u id,但当它位于zfcUser中且该类是映射超类时,它应该可以工作。