Bonjour,

J'ai besoin d'obtenir cette requete SQL, mais qui me retournerai pour chaque booking : l'identifiant associé a event_id. (l'id Event)
Qui est compétent pour m'aider ?
Aggr, il est deja minuit. Un grand Merci a celui qui trouve.

$bookings = $bookingTable->listBookingsByAllEvents( [966, 972, 940] );

public function listBookingsByAllEvents(array $listEventId){

    $sql = "SELECT b.*
    FROM {$this->table} b 
    JOIN booking_event be ON be.booking_id = b.id 
    WHERE be.event_id IN (" . implode(', ', $listEventId) . ")" ;

    $statement =  $this->pdo->query($sql);
    $statement->setFetchMode(\PDO::FETCH_CLASS, Booking);
    $bookings = $statement->fetchAll();

    return $bookings;

}

Pour le moment j'obtiens ca :

array (size=12)
0 =>
object(App\Model\Booking)[29]
private 'id' => string '224' (length=3)
private 'fullName' => string '' (length=0)
private 'phone' => string '' (length=0)
private 'email' => string 'exemple@gmail.com' (length=21)

1 =>
object(App\Model\Booking)[30]
private 'id' => string '225' (length=3)
private 'fullName' => string '' (length=0)
private 'phone' => string '' (length=0)
private 'email' => string 'exemple@gmail.com' (length=21)

A la place des clefs, j'ai besoin d'associer chaque booking a une clef event_id
(les event_id sont disponibles a la fois dans ma table de jointure booking_event et dans le param $listEventId)
Un grand merci

3 réponses


Bastien658
Auteur
Réponse acceptée

Au final avec 2 foreach, je formates mes resultats.
Merci en tout cas pour la requete.

Hello :)

Alors pour récupérer une valeur d'une relation, il faut l'ajouter au select:

 $sql = "SELECT b.*, be.event_id
    FROM {$this->table} b
    JOIN booking_event be ON be.booking_id = b.id 
    WHERE be.event_id IN (" . implode(', ', $listEventId) . ")" ;

    $statement =  $this->pdo->query($sql);
    $statement->setFetchMode(\PDO::FETCH_CLASS, Booking);
    $bookings = $statement->fetchAll();

    return $bookings;

Ah c'est trop bien, ca marche, merci !

En fait, c'était pas si compliquer.
Par contre ca m'ajoute l'ID Event, à la suite de mon objet Booking, comme une simple valeur.
J'ai besoin dans mon php de pouvoir appeler efficacement l'ID d'un Event, qui contiendra les differents Booking associés.

Vous savez si c'est possible en SQL de faire une partie de ca ?
Car, coder un algo pour faire cela, ca va etre compliquer pour moi.

Mais aussi non, merci, la problematique initial est résolu. Merci.

array (size=12)
0 => 
object(App\Model\Booking)[29]
  private 'id' => string '224' (length=3)
  private 'fullName' => string '' (length=0)
  private 'phone' => string '' (length=0)
  private 'email' => string 'bastien.riv@gmail.com' (length=21)
  private 'numberOfTickets' => string '1' (length=1)
  public 'event_id' => string '940' (length=3)
1 => 
object(App\Model\Booking)[30]
  private 'id' => string '225' (length=3)
  private 'fullName' => string '' (length=0)
  private 'phone' => string '' (length=0)
  private 'email' => string 'bastien.riv@gmail.com' (length=21)
  private 'numberOfTickets' => string '1' (length=1)
  public 'event_id' => string '940' (length=3)
  3 =>

  Et si j'ajoute un attribut supplémentaire dans mon modele Booking, mon event_id serait automatiquement intégré à l'objet ? Ca m'aiderai pas beaucoup plus a formater mes donées je suppose.