原则问题(映射不一致) [英] Doctrine issue (mappings inconsistent)

查看:107
本文介绍了原则问题(映射不一致)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Symfony的一个项目中忙着,我只是检查分析器选项卡,看到2个错误不断弹出 - 它们在下面。

 映射MyBundle\MainBundle\Entity\School#provinceId和MyBundle\MainBundle\Entity\Province#学校彼此不一致。 

关联MyBundle\MainBundle\Entity\School#等级是指拥有的边域MyBundle\MainBundle\Entity\Grade#school_id不存在。

我得到了更多的这些,我不明白为什么? 不一致是什么意思(看我在那里做什么)?如果有帮助,我的代码部分将在下面。



在Province.php

  / ** 
* @ ORM\OneToMany(targetEntity =School,mappedBy =provinceId)
* /
private $学校;

和我的Schools.php

  / ** 
* @var整数
*
* @ ORM\ManyToOne(targetEntity =省,inversedBy =学校)
* @ ORM\JoinColumn(name =province_id,referencedColumnName =id)
* /
private $ provinceId;

第二个错误...



School.php

  / ** 
* @ ORM\OneToMany(targetEntity =Grade,mappedBy =school_id)
* /
private $ grade;

和Grade.php

  / ** 
* @var integer
*
* @ ORM\ManyToOne(targetEntity =School,inversedBy =grade)
* @ ORM\JoinColumn(name =school_id,referencedColumnName =id)
* /
private $ schoolId;

我只想知道这些错误是什么意思,为什么这些实体是不对的 - 我试过遵循教条页面的文档,但显然我在某个地方出错!



感谢任何帮助!

解决方案

我没有你的整个配置,所以我只是在这里做一个有教养的猜测...(原谅我,如果我错了!)



关于第一个,你说映射如下所示:

 #Province.php 
/ **
* @ ORM\OneToMany(targetEntity =School,mappedBy =provinceId)
* /
private $学校;

#School.php
/ **
* @var整数
*
* @ ORM\ManyToOne(targetEntity =省,inversedBy =schools)
* @ ORM\JoinColumn(name =province_id,referencedColumnName =id)
* /
private $ provinceId;

我想象,这是在这里抛出的东西。你看,映射的目的是让你可以对待这些东西,比如对象,而不必担心如何在数据库中保持/连接。具体来说,在你的情况下,一个学校实体不应该有一个成员 $ provinceId 类型为整数;相反,它应该有一个省级



尝试这样:

 #Province.php 
/ **
* @ ORM\ OneToMany(targetEntity =School,mappedBy =province)
* /
private $学校;

#School.php
/ **
* @var省
*
* @ ORM\ManyToOne(targetEntity =省,inversedBy =学校)
* @ ORM\JoinColumn(name =province_id,referencedColumnName =id)
* /
private $ province;

(再次,这是完全未经测试的,我只有一部分你有...但我认为这会让你更接近。)


I'm busy with a project in Symfony and I'm just checking the profiler tab and seeing 2 errors continuously popping up - they are below.

The mappings MyBundle\MainBundle\Entity\School#provinceId and MyBundle\MainBundle\Entity\Province#schools are incosistent with each other.

The association MyBundle\MainBundle\Entity\School#grades refers to the owning side field MyBundle\MainBundle\Entity\Grade#school_id which does not exist.

I'm getting a couple more of these and I can't understand why? What does it mean by "incosistent" (see what I did there)? Parts of my code is below if it's helpful.

In Province.php

/**
  * @ORM\OneToMany(targetEntity="School", mappedBy="provinceId")
 */ 
private $schools;

and in my Schools.php

/**
 * @var integer
 *
 * @ORM\ManyToOne(targetEntity="Province", inversedBy="schools")
 * @ORM\JoinColumn(name="province_id", referencedColumnName="id")
 */
private $provinceId;

And for the second error...

School.php

/**
 * @ORM\OneToMany(targetEntity="Grade", mappedBy="school_id")
 */
private $grades;

and Grade.php

/**
 * @var integer
 *
 * @ORM\ManyToOne(targetEntity="School", inversedBy="grades")
 * @ORM\JoinColumn(name="school_id", referencedColumnName="id")
 */
private $schoolId;

I just want to know what these errors mean exactly and why these entities aren't right - I tried following the docs off the doctrine page but apparently I went wrong somewhere!

Thanks for any help!

解决方案

I don't have your entire configuration, so I'm just going to make an educated guess here... (forgive me if I'm wrong!)

Regarding the first one, you say the mapping looks like this:

# Province.php
/**
  * @ORM\OneToMany(targetEntity="School", mappedBy="provinceId")
 */ 
private $schools;

# School.php
/**
 * @var integer
 *
 * @ORM\ManyToOne(targetEntity="Province", inversedBy="schools")
 * @ORM\JoinColumn(name="province_id", referencedColumnName="id")
 */
private $provinceId;

I imagine it's the types that are throwing things off here. You see, the purpose of the mappings is so that you can treat these things like objects, without having to worry about how they're persisted/connected in the database. Specifically, in your case, a School entity should not have a member $provinceId of type integer; rather, it should have a $province of type Province.

Try this:

# Province.php
/**
  * @ORM\OneToMany(targetEntity="School", mappedBy="province")
 */ 
private $schools;

# School.php
/**
 * @var Province
 *
 * @ORM\ManyToOne(targetEntity="Province", inversedBy="schools")
 * @ORM\JoinColumn(name="province_id", referencedColumnName="id")
 */
private $province;

(Again, this is wholly untested, and I only have a part of what you have... but I think this will get you closer.)

这篇关于原则问题(映射不一致)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆