如何在symfony中添加外键(一对多)

时间:2019-03-22 19:19:48

标签: symfony doctrine foreign-keys entity

我有两个实体'Panier'和'Reservation',我想向多个外键添加一个(实体'Panier'可以具有多个'Reservation',而'Reservation'只有一个'Panier'id),所以我必须在我的Reservation实体类中添加“ Panier”的外键“ id”。 这是我的预订课程:

班级预订 {     / **      * @var整数      *      * @ORM \ Column(name =“ id”,type =“ integer”,nullable = false)      * @ORM \ Id      * @ORM \ GeneratedValue(strategy =“ IDENTITY”)      * /     私人$ id;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="dateReservation", type="datetime", nullable=false)
 */
private $datereservation = 'CURRENT_TIMESTAMP';

/**
 * @var integer
 *
 * @ORM\Column(name="quantite", type="integer", nullable=false)
 */
private $quantite;

/**
 * @var float
 *
 * @ORM\Column(name="total", type="float", precision=10, scale=0, nullable=true)
 */
private $total;

/**
 * @var string
 *
 * @ORM\Column(name="type", type="string", length=255, nullable=false)
 */
private $type;

/**
 * @var string
 *
 * @ORM\Column(name="seat", type="string", length=255, nullable=false)
 */
private $seat;

/**
 * @var integer
 *
 * @ORM\Column(name="payer", type="integer", nullable=true)
 */
private $payer;

/**
 * @var string
 *
 * @ORM\Column(name="nomReservation", type="string", length=255, nullable=true)
 */
private $nomreservation;

/**
 * @var \Event
 *
 * @ORM\ManyToOne(targetEntity="Event")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="event_id", referencedColumnName="id")
 * })
 */
private $event;

/**
 * @var \User
 *
 * @ORM\ManyToOne(targetEntity="User")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 * })
 */
private $user;

1 个答案:

答案 0 :(得分:0)

<?php

/** @Entity */
class Reservation {

  /**
   * @ManyToOne(targetEntity="Panier", inversedBy="reservations")
   * @JoinColumn(name="panier_id", referencedColumnName="id")
   */
   private $panier;

}

/** @Entity */
class Panier {

   /**
    * One Panier has many Reservations. This is the inverse side.
    * @OneToMany(targetEntity="Reservation", mappedBy="panier")
    */
    private $reservations;

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