1-1 Unidirectional relation Doctrine 2 ORM


Description


use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="product")
 */
class Product {
    /**
     * @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue
     * @var int
     */
    private $id;

    /**
     * @ORM\OneToOne(targetEntity="Shipment")
     * @ORM\JoinColumn(name="shipment_id", referencedColumnName="id")
     */
    private $shipment;
}

/**
 * @ORM\Entity
 * @ORM\Table(name="shipment")
 */
class Shipment {
    /**
     * @ORM\Id @ORM\Column(type="integer") @ORM\GeneratedValue
     * @var int
     */
    private $id;
}
The above @JoinColumn is optional as it would default to shipment_id and id anyways. <br />
<br />
<b>Table Product</b> <br />
- <b>id:</b> PK, Auto Increment <br />
- <b>shipment_id:</b> Unique, FK Shipment(id) <br />
<br />
<b>Table Shipment</b> <br />
- <b>id:</b> PK, Auto Increment <br />
SEE ALSO