首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >关联失败,原理映射原理2

关联失败,原理映射原理2
EN

Stack Overflow用户
提问于 2013-04-16 20:43:44
回答 1查看 2.8K关注 0票数 0

我有以下关于规则2的问题,我想将我的地址实体和国家实体加入到我的用户实体中。所以经过几次尝试之后,它在数据库中工作得很好,但是当我使用./vendor/bin/doctrine-module orm:validate-schema时,它会显示一些映射失败。

代码语言:javascript
复制
[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.

我真的不知道为什么会发生这种情况,因为我的数据库像这样工作得很好。它识别地址中的用户和国家/地区实体,以及值。

我的用户实体:

代码语言:javascript
复制
   ...
    /**
 * 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;

    ...
    ...

我的地址实体:

代码语言:javascript
复制
/**
 * 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;

...
...

我的国家/地区实体:

代码语言:javascript
复制
   ...
    ...
    /**
 * 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;

    ...
    ...

有人能解释为什么会发生这种事吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-16 23:37:41

对于第一个错误:

在您的User类中,缺少与address相关的属性,因此可能有两种可能性:

  1. If in protected $user_id:这不起作用,因为Doctrine需要在此位置有一个address对象数组。
  2. If in protected $addresses:您必须将user属性上的Address类中的映射更改为:

@ORM\ManyToOne(targetEntity="User", inversedBy="addresses")

对于第二个错误:

Address类中,您必须删除country上的inversedBy属性:

代码语言:javascript
复制
@ORM\ManyToOne(targetEntity="Country")
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16037394

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档