我必须重写从zf1到sf2的应用程序。 但我需要保留旧的数据库模式。 我有许多关系的问题。与保持旧数据库的多对多关系
有2个实体:异常,区域,它也是Exceptionregions,但我删除它。 数据库中有3个表 - 异常,区域和异常区域,这是散列表。 下面附上屏幕关系:
我的代码: 1.异常实体:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Exceptions
*
* @ORM\Table(name="Exceptions")
* @ORM\Entity
*/
class Exceptions
{
/**
* @var integer
*
* @ORM\Column(name="ExceptionID", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $exceptionid;
/**
* Many exceptions have many regions.
* @ORM\ManyToMany(targetEntity="Regions", inversedBy="exceptions")
* @ORM\JoinTable(name="exceptionregions"),
* joinColumns={@ORM\JoinColumn(name="ExceptionID", referencedColumnName="ExceptionID")},
* inverseJoinColumns={@ORM\JoinColumn(name="RegionID", referencedColumnName="RegionID")}
*)
*/
private $regions;
public function __construct()
{
$this->regions = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add region
*
* @param AppBundle\Entity\Regions $region
*/
public function addRegion(\AppBundle\Entity\Regions $regions)
{
$this->regions[] = $regions;
}
/**
* Get regions
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getRegions()
{
return $this->regions;
}
...
}
地区实体:
<?php namespace AppBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * Regions * * @ORM\Table(name="Regions") * @ORM\Entity */ class Regions { /** * @var string * * @ORM\Column(name="RegionName", type="string", length=45, nullable=false) */ private $regionname; /** * @var integer * * @ORM\Column(name="RegionID", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") */ private $regionid; /** * @ORM\ManyToMany(targetEntity="Exceptions", mappedBy="regions") */ private $exceptions; ... }
而我得到这个错误:
The column id must be mapped to a field in class AppBundle\Entity\Exceptions since it is referenced by a join column of another class.
当然,实体异常与少数实体连接,而不仅仅是区域。 我被这个问题困住了,我无法解决这个问题并继续我的项目。 任何人有任何想法如何修复这个或任何建议?我究竟做错了什么? 我很感激任何评论。
我没有看到在代码中任何地方定义的列标识。要么你没有向我们展示代码,或者你没有清除缓存。使用:'php bin/console cache:clear --env = prod'清除你的生产缓存。 –
@Alvin Bunk感谢您的评论。缓存被清除。这些表中的ID被称为RegionID和ExceptionID(在实体属性中:regionid和exceptionid)。我知道问题可能与ID的名称有关,但我无法改变它们。该如何处理这些名称? – r3m4k3