Thomas Good Thomas Good - 6 months ago 24
MySQL Question

Doctrine Join tables to get the same ID

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

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:

Table reservation:

Reservation id = "1"

Reservation id = "2"

Table Billet:

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

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

Billet 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="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;
}

}

Answer

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

}