Doctrine加入表以获取相同的ID

时间:2016-05-24 18:20:29

标签: php mysql symfony doctrine-orm

我是symfony的初学者,我必须使用收集表格建立一个网站来预订节目门票。

界面很简单,用户选择他想要的门票数量,然后显示尽可能多的表格原型(姓名和姓氏输入)作为所需的门票。这部分适合我。

每次提交=新预订

我有2张桌子"预订"和"票"在mysql中,我希望在提交表单后获得此渲染:

表格预订:

预订ID =" 1"

预订ID =" 2"

表坯:

方坯ID =" 1" name =" thomas",surname =" good",reservation_id =" 1"

方坯ID =" 2" name =" laura",surname =" senior",reservation_id =" 1"

方坯ID =" 3" name =" pierre",surname =" martin",reservation_id =" 2"

问题是我得到了这个结果:

方坯ID =" 1" name =" thomas",surname =" good",reservation_id =" Null"

方坯ID =" 2" name =" laura",surname =" senior",reservation_id =" Null"

这是我的实体:

<?php

namespace Louvre\PagesBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Billet
 *
 * @ORM\Table(name="billet")
 * @ORM\Entity(repositoryClass="Louvre\PagesBundle\Repository\BilletRepository")
 */
class Billet
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Reservations", inversedBy="billets")
     * @ORM\JoinColumn(name="reservations_id", referencedColumnName="id")
     */
    protected $reservations;

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

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


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set prenom
     *
     * @param string $prenom
     * @return Billet
     */
    public function setPrenom($prenom)
    {
        $this->prenom = $prenom;

        return $this;
    }

    /**
     * Get prenom
     *
     * @return string 
     */
    public function getPrenom()
    {
        return $this->prenom;
    }

    /**
     * Set nom
     *
     * @param string $nom
     * @return Billet
     */
    public function setNom($nom)
    {
        $this->nom = $nom;

        return $this;
    }

    /**
     * Get nom
     *
     * @return string 
     */
    public function getNom()
    {
        return $this->nom;
    }
}


<?php

namespace Louvre\PagesBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

use Doctrine\Common\Collections\ArrayCollection;

/**
 * Reservations
 *
 * @ORM\Table(name="reservations")
 * @ORM\Entity(repositoryClass="Louvre\PagesBundle\Repository\ReservationsRepository")
 */
class Reservations
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }


    /**
     * @ORM\OneToMany(targetEntity="Billet", mappedBy="reservations", cascade={"persist","remove"})
     */
    protected $billets;


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

    public function getBillets()
    {
        return $this->billets;
    }

    public function setBillets(ArrayCollection $billets)
    {
        $this->billets = $billets;
    }

}

1 个答案:

答案 0 :(得分:1)

你的命名有点困惑......

我建议:

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

/**
 * @ORM\ManyToOne(targetEntity="Reservation", inversedBy="billets")
 * @ORM\JoinColumn(name="reservation_id", referencedColumnName="id")
 */
protected $reservation;

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

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


/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * Set prenom
 *
 * @param string $prenom
 * @return Billet
 */
public function setPrenom($prenom)
{
    $this->prenom = $prenom;

    return $this;
}

/**
 * Get prenom
 *
 * @return string 
 */
public function getPrenom()
{
    return $this->prenom;
}

/**
 * Set nom
 *
 * @param string $nom
 * @return Billet
 */
public function setNom($nom)
{
    $this->nom = $nom;

    return $this;
}

/**
 * Get nom
 *
 * @return string 
 */
public function getNom()
{
    return $this->nom;
}

public function setReservation(Reservation $reservation)
{
    $this->reservation = $reservation;

    return $this;
}

public function getReservation()
{
    return $this->reservation;
}
}  

Reservation实体

/**
 * Reservation
 *
 * @ORM\Table(name="reservations")
 * @ORM\Entity(repositoryClass="Louvre\PagesBundle\Repository\ReservationsRepository")
 */
class Reservation
{
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;


/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}


/**
 * @ORM\OneToMany(targetEntity="Billet", mappedBy="reservations", cascade={"persist","remove"})
 */
protected $billets;


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

public function getBillets()
{
    return $this->billets;
}

public function addBillet(Billet $billet)
{
    if (!$this->billets->contains($billet))
    {
        $this->billets->add($billet);
        $billet->setReservation($this);
    }

    return $this;
}

public function removeBillet(Billet $billet)
{
    // you can implement for collection
}

public function setBillets($billets)
{
    $this->billets = new ArrayCollection();

    foreach ($billets as $billet)
    {
        $this->addBillet($billet)
    }

    return $this;
}

}