2016-12-31 128 views
1

我必须重写从zf1到sf2的应用程序。 但我需要保留旧的数据库模式。 我有许多关系的问题。与保持旧数据库的多对多关系

有2个实体:异常,区域,它也是Exceptionregions,但我删除它。 数据库中有3个表 - 异常,区域和异常区域,这是散列表。 下面附上屏幕关系:

screen with relations

我的代码: 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. 
    

    当然,实体异常与少数实体连接,而不仅仅是区域。 我被这个问题困住了,我无法解决这个问题并继续我的项目。 任何人有任何想法如何修复这个或任何建议?我究竟做错了什么? 我很感激任何评论。

    +0

    我没有看到在代码中任何地方定义的列标识。要么你没有向我们展示代码,或者你没有清除缓存。使用:'php bin/console cache:clear --env = prod'清除你的生产缓存。 –

    +0

    @Alvin Bunk感谢您的评论。缓存被清除。这些表中的ID被称为RegionID和ExceptionID(在实体属性中:regionid和exceptionid)。我知道问题可能与ID的名称有关,但我无法改变它们。该如何处理这些名称? – r3m4k3

    回答

    0

    我找到了解决这个问题的办法。 也许有人会从中受益。

    因此,工作代码:

    /** 
    * Many exceptions have many regions. 
    * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Regions", inversedBy="exceptions") 
    * @ORM\JoinTable(name="exceptionregions", 
    * joinColumns={ 
    *  @ORM\JoinColumn(name="ExceptionID", referencedColumnName="ExceptionID") 
    * }, 
    * inverseJoinColumns={ 
    *  @ORM\JoinColumn(name="RegionID", referencedColumnName="RegionID") 
    * }) 
    */ 
    private $regions; 
    

    @Alvin,感谢您的承诺。

    0

    你有没有试图使关系双向像这样:

    class Exceptions{ 
    ... 
        /** 
        * Many exceptions have many regions. 
        * @ORM\ManyToMany(targetEntity="Regions", inversedBy="exceptions") 
        * @ORM\JoinTable(name="regions_exceptions") 
        */ 
        private $regions; 
    

    class Regions{ 
    ... 
        /** 
        * @ORM\ManyToMany(targetEntity="Exceptions", mappedBy="regions") 
        */ 
        private $exceptions; 
    

    不知道这会的工作,但你可以试试。

    文档浏览: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#many-to-many-bidirectional

    **编辑#2 ** 你可以试试这个变化:

    class Exceptions 
    { 
    
        /** 
        * @var integer 
        * 
        * @ORM\Id 
        * @ORM\Column(name="ExceptionID", type="integer") 
        * @ORM\GeneratedValue(strategy="AUTO") 
        */ 
        protected $exceptionid; 
    

    而且,如果还是不行,请尝试:

    php bin/console doctrine:schema:update --force 
    
    +0

    是的,我尝试过双向和单向关系。两者都给了我相同的结果,我上面提到的错误。在我看来,问题是ID的名称。 – r3m4k3

    +0

    我做了一个编辑。你可以尝试编辑#2的变化。只是想帮助。我对这个问题感到不知所措。 –

    +0

    嗨,谢谢你的帮助,我很欣赏这一点。我也很困惑。但不幸的是,改变变量的作用域也不起作用。更新模式给了我相同的结果。 – r3m4k3