Doctrine连接表以获取相同的ID

时间:2022-11-06 06:52:45

I'm beginner on symfony and I have to use the collection form to make a site to reserve tickets for a show.

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

The interface is simple, The user select the number of tickets he wants, then it display as much form prototype (name and surname input) as tickets required. This part works well for me.

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

Every submit = new reservation

每次提交=新预订

I have 2 table "reservations" and "ticket" in mysql and I would like to get this render after submiting the form:

我在mysql中有2个表“预订”和“票证”,我希望在提交表单后获得此渲染:

Table reservation:

Reservation id = "1"

预订ID =“1”

Reservation id = "2"

预订ID =“2”

Table Billet:

Billet id="1" name="thomas", surname="good", reservation_id="1"

方坯id =“1”name =“thomas”,surname =“good”,reservation_id =“1”

Billet id="2" name="laura", surname="senior", reservation_id="1"

Billet id =“2”name =“laura”,surname =“senior”,reservation_id =“1”

Billet id="3" name="pierre", surname="martin", reservation_id="2"

方坯id =“3”name =“pierre”,surname =“martin”,reservation_id =“2”


Problem is that i get this result :

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

Billet id="1" name="thomas", surname="good", reservation_id="Null"

Billet id =“1”name =“thomas”,surname =“good”,reservation_id =“Null”

Billet id="2" name="laura", surname="senior", reservation_id="Null"

Billet id =“2”name =“laura”,surname =“senior”,reservation_id =“Null”


Here is my entitys:

这是我的实体:

<?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 个解决方案

#1


1  

Your naming is a little confused ...

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

I suggest that :

我建议 :

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;
}
}  

The Reservation entity

预订实体

/**
 * 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;
}

}   

#1


1  

Your naming is a little confused ...

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

I suggest that :

我建议 :

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;
}
}  

The Reservation entity

预订实体

/**
 * 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;
}

}