I am working on the Doctrine ORM (2.4.8, I know, it's old I cannot do anything about it right now) part of the code (Symfony 3 again, I'm aware it's old). I need many-to-many relation here because multiple Countries might be listed in multiple LineItems. The primary key of the Country table is iso3 and I would not like to modify it.
Here is the field that should contain the countries:
class LineItem
{
...
/**
* @ORM\ManyToMany(targetEntity="CampaignBundle\...\Country", inversedBy="iso3")
* @ORM\JoinTable(
* name="lineitem_country_filter",
* joinColumns={
* @ORM\JoinColumn(name="country_id", referencedColumnName="iso3")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="lineitem_id", referencedColumnName="id")
* }
* )
*/
private $countryFilter;
...}
Here is a part of the Country class:
class Country
{...
/**
* @ORM\Column(name="iso3", type="string", length=10, unique=true)
* @ORM\Id()
* @ORM\ManyToMany(targetEntity="LineItem", mappedBy="countryFilter")
* @ORM\OneToMany(targetEntity="Region", mappedBy="country")
* @ORM\GeneratedValue(strategy="NONE")
*/
private $iso3;
...}
When I try to run symfony console d:m:diff
I get this issue:
In SchemaTool.php line 648:
Column name 'iso3' referenced for relation from CampaignBundle\...\LineItem towards CampaignBundle\...\Country does not exist.
I have no idea how to solve it. Just for a small clarification, I'm a junior dev in a small company ;)
Thanks to – Nuryagdy Mustapayev, it works now. I've changed the LineItem code as follows:
* @ORM\ManyToMany(targetEntity="CampaignBundle\...\Country")
* @ORM\JoinTable(name="lineitem_country_filter",
* joinColumns={@ORM\JoinColumn(name="lineitem_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="country_id", referencedColumnName="iso3")})
Removed the @ORM\ManyToMany
from Country
part, because as Nuryagdy mentioned it's unidirectional relation.
The CountryNA was my mistake, I forget to remove 'NA' so it's more readable, now it's fixed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.