Relationship in doctrine

0

I have a problem that is analogous to the following situation:

A car may or may not have a driver, ie a car may have a minimum of 0 and a maximum of 1 driver.

A driver may or may not have a car, ie a driver can have a minimum of 0 and a maximum of 1 car.

  

In the example in question we are accepting that the driver may have   maximum one car.

Turning this into a class would then have two classes.

<?php 
class Carro{
    /**
    * esse deve aceitar null
    */
    private $motorista;
}


class Motorista{
    /**
    * esse deve aceitar null
    */
    private $carro;
}

In my view, the mapping that would interest me most would be OneToOne, but I do not know if it should be OneToMany or ManyToOne, if it were possible to get it to accept null values, however, I would like to know what is the best way to do it doctrine and the most correct way according to Designer Partner (I wonder why it might be that the best way to do it using the doctrine is not the right way).

    
asked by anonymous 17.07.2014 / 05:36

1 answer

1

For me, the Carro class would have a $motorista attribute, just as the Motorista class would have the $carro attribute (resulting in a one-to-one, bidirectional relationship). Both attributes would be nullable .

So you would have a Car table and a Driver table, and the Driver table has a column referencing Car.

Note that this restriction would only be at the level of your application - if someone wanted to enter the data directly into the bank, override the constraint, could.

<?php

/**
 * @ORM\Entity
 */
class Carro
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @OneToOne(targetEntity="Motorista", mappedBy="carro")
     * @JoinColumn(nullable=true)
     */
    private $motorista;
}

/**
 * @ORM\Entity
 */
class Motorista
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @OneToOne(targetEntity="Carro", inversedBy="motorista")
     * @JoinColumn(nullable=true)
     */
    private $carro;
}
    
20.07.2014 / 01:00