Home > other >  ORM Doctrine - JoinTable and JoinColumn "column not found" or "could not resolve type
ORM Doctrine - JoinTable and JoinColumn "column not found" or "could not resolve type

Time:03-08

I am a begginer in Doctrine and i am stuck in kind of a loop trying to establish a join table between 2 entities.

I have a relation OneToMany between a "train" and "containers" and ManyToOne between "containers" and "train" obviously.

I placed annotations on the OneToMany relation like this :

@ORM\Entity/Train

@ORM\OneToMany(targetEntity=Container::class, mappedBy="Train")
@ORM\JoinTable(name="train_container", 
@ORM\joinColumns={@JoinColumn(name="id_tm", referencedColumnName="id_tm", nullable=false)},
@ORM\inverseJoinColumns={@JoinColumn(name="id_container", referencedColumnName="id_container", nullable=false)})
      )
    
private $Container;

and on the ManyToOne i did like this :

@ORM\Entity/Container 

@ORM\ManyToOne(targetEntity=Train::class, inversedBy="Containers")
@ORM\JoinColumn(name="id_tm", referencedColumnName="id_tm")
private $Train;

Now, if i let it like that, i have an error saying :

Annotation @ORM\joinColumns is not allowed to be declared on property App\Entity\Train::$container. You may only use this annotation on these code elements: PROPERTY.

If i remove the joinColumn and just put this :

@ORM\ManyToOne(targetEntity=Train::class, inversedBy="Container")
private $Train;

I have this error :

Could not resolve type of column "id" of class "App\Entity\Train"

I have no idea why its displaying an "id" column because the name of the column and the property on "Train" is always "id_tm" everywhere.

Could anyone tell me what i am doing wrong please ?

thanks a lot in advance M

edit to add the code on the id's properties :

@ORM\Id
@ORM\GeneratedValue
@ORM\Column(type="integer", name="id_container")

private $id_container;
@ORM\Id
@ORM\GeneratedValue
@ORM\Column(type="integer", name="id_train")

private $id_train;

CodePudding user response:

On the Train entity, you need to use the ManyToMany annotation with the unique parameter to get a OneToMany association as shown in the doctine documentation.

Here is the code that worked for me, try this:

Train Entity

/**
 * Class Train
 * @package App\Entity
 * @ORM\Entity()
 * @ORM\Table(name="train", options={"comment":"Train"})
 */
class Train
{

    /**
     * @var int
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(name="id_train", type="integer", unique=true)
     */
    private int $id_train;


    /**
     * @ORM\ManyToMany(targetEntity="App\Entity\Container", mappedBy="Train", cascade={"persist"})
     * @ORM\JoinTable(name="train_container",
     *     joinColumns={@ORM\JoinColumn(name="train_id", referencedColumnName="id_train")},
     *     inverseJoinColumns={@ORM\JoinColumn(name="container_id", referencedColumnName="id_container", unique=true)}
     * )
     */
    private $Container;


    public function __construct() {
        $this->Container = new ArrayCollection();
    }
    
    //....

Also, if you want to have a bidirectional relationship between Container and Train entities, then you need to specify the following in the Container entity.

Container Entity

/**
 * Class Container
 * @package App\Entity
 * @ORM\Entity()
 * @ORM\Table(name="container", options={"comment":"Container"})
 */
class Container
{

    /**
     * @var int
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(name="id_container", type="integer", unique=true)
     */
    private int $id;


    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Train", inversedBy="Container")
     * @ORM\JoinColumn(name="train_id", referencedColumnName="id_train")
     */
    private $Train;

Don't forget to generate getters and setters after

 php bin/console make:entity --regenerate

And update schema

php bin/console d:s:u -f
  • Related