I have to rewrite application from zf1 to sf2. But I need to keep old database schema. And I have problem with many to many relations.
There are 2 entities: Exceptions, Regions and it was too Exceptionregions, but I deleted it. There are 3 tables in database - exceptions, regions and exceptionregions, which is hash table. Below I attach screen with relations:
My code: 1. Exception entity:
<?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;
}
...
}
Region entity:
<?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;
...
}
And I got this error:
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.
Of course entity Exceptions is connected with few entities, not only regions. I got stuck with this issue, I can't resolve this problem and continue my project. Anybody has any idea how to repair this or any advice? What am I doing wrong? I'd be grateful for any comment.
I found a solution for this problem. Maybe someone will benefit from this too.
So, the working code:
/**
* 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, thank you for your commitment.