0

I'm trying to create a "OneToMany" bidirectional association in my project but when I execute "doctrine:schema:update" nothing happens.

If I create this association directly from Sequel Pro and run the update schema command, that changes dissapear... :/

The relations is: - One "id" from Customers Table with many "customer_id" form Control table.

Here is the Customers code:

<?php namespace Ourentec\CustomersBundle\Entity; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; /** * Customers * * @ORM\Table() * @ORM\Entity */ class Customers { /* @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string * * @ORM\Column(name="name", type="string", length=100) */ private $name; /** * @var string * * @ORM\Column(name="lastname", type="string", length=100) */ private $lastname; /** * @var string * * @ORM\Column(name="address", type="text") */ private $address; /** * @var string * * @ORM\Column(name="phone", type="string", length=100) */ private $phone; /** * @var string * * @ORM\Column(name="pass", type="string", length=100) */ private $pass; /** * @var string * * @ORM\Column(name="tasks", type="text") */ private $tasks; /** * @var string * * @ORM\Column(name="status", type="string", length=100) */ private $status; /** * @var string * * @ORM\Column(name="email", type="string", length=100) */ private $email; /** * @var \DateTime * * @ORM\Column(name="date", type="datetime") */ private $date; /** * @var string * * @ORM\Column(name="location", type="string", length=100) */ private $location; /** * @ORM\OneToMany(targetEntity="Control", mappedBy="customers") */ private $customer_id; public function __construct() { $this->customer_id = new ArrayCollection(); } 

And the Control code:

<?php namespace Ourentec\CustomersBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * Control * * @ORM\Table() * @ORM\Entity */ class Control { /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var integer * * @ORM\Column(name="customer_id", type="integer") * * @ORM\ManyToOne(targetEntity="Customers", inversedBy="control") * @ORM\JoinColumn(name="customer_id", referencedColumnName="id") */ private $customerId; /** * @var integer * * @ORM\Column(name="user_id", type="integer") */ private $userId; /** * @var \DateTime * * @ORM\Column(name="date", type="datetime") */ private $date; /** * @var integer * * @ORM\Column(name="seen", type="smallint") */ private $seen; 

I followed the documentation from this 2 websites http://symfony.com/doc/current/book/doctrine.html http://librosweb.es/libro/symfony_2_x/capitulo_8/relaciones_y_asociaciones_de_entidades.html

But I don't know why it does not work..

Any idea will be appreciated :)

1 Answer 1

3

Mapping are not correct, I will try to explain how it works.

In Customers entity (you should rename it to Customer, entites names are singular)

/** * @ORM\OneToMany(targetEntity="Control", mappedBy="customer") */ private $controls; 

Mapped by option defines field name in the other entity.

/** * @var integer * * @ORM\Column(name="customer_id", type="integer") * * @ORM\ManyToOne(targetEntity="Customers", inversedBy="controls") * @ORM\JoinColumn(name="customer_id", referencedColumnName="id") */ private $customer; 

Same thing with inversedBy.

In Customers entity you also need to init controls var as an ArrayCollection:

public function __construct() { $this->controls = new ArrayCollection(); } 

With these mappings schema should be updated correctly.

For more info, check doctrine docs.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm glad to hear that :)