Voila je rencontre un petit problème avec mon code.
Ce que je fais
J'ai table : table ticket et table voyage. La table ticket prend l'id du voyage.
Je voulais creer un ticket, mais l'erreur suivante s'affiche : Object of class App\Entity\TrVoyage could not be converted to string.
Ce qui veut dire de mettre
public function __toString()
{
return $this->le libelle de mon champ voyage;
}
Je comprends l'erreur, seulement que la table voyage ne contient pas de champs en string, mais des champs en date et int
voici mon code
table ticket
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* TrTicket
*
* @ORM\Table(name="tr_ticket", indexes={@ORM\Index(name="VOYAGE_idx", columns={"V_ID"})})
* @ORM\Entity(repositoryClass="App\Repository\TrTicketRepository")
*/
class TrTicket
{
/**
* @var int
*
* @ORM\Column(name="TCK_ID", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $tckId;
/**
* @var string
*
* @ORM\Column(name="TCK_NUM", type="string", length=200, nullable=false)
*/
private $tckNum;
/**
* @var string|null
*
* @ORM\Column(name="TCK_LIB", type="string", length=200, nullable=true)
*/
private $tckLib;
/**
* @var int
*
* @ORM\Column(name="TCK_PRIX", type="integer", nullable=false)
*/
private $tckPrix;
/**
* @var bool|null
*
* @ORM\Column(name="TCK_FLAG", type="boolean", nullable=true, options={"default"="1"})
*/
private $tckFlag = true;
/**
* @var \TrVoyage
*
* @ORM\ManyToOne(targetEntity="TrVoyage")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="V_ID", referencedColumnName="V_ID")
* })
*/
private $v;
public function getTckId(): ?int
{
return $this->tckId;
}
public function getTckNum(): ?string
{
return $this->tckNum;
}
public function setTckNum(string $tckNum): self
{
$this->tckNum = $tckNum;
return $this;
}
public function getTckLib(): ?string
{
return $this->tckLib;
}
public function setTckLib(?string $tckLib): self
{
$this->tckLib = $tckLib;
return $this;
}
public function getTckPrix(): ?int
{
return $this->tckPrix;
}
public function setTckPrix(int $tckPrix): self
{
$this->tckPrix = $tckPrix;
return $this;
}
public function getTckFlag(): ?bool
{
return $this->tckFlag;
}
public function setTckFlag(?bool $tckFlag): self
{
$this->tckFlag = $tckFlag;
return $this;
}
public function getV(): ?TrVoyage
{
return $this->v;
}
public function setV(?TrVoyage $v): self
{
$this->v = $v;
return $this;
}
// comment transformer ce qui est juste en dessous par : $result = $this->vDate . "/" . $this->vHeuredep . "-" . $this->vHeurearv; pour que ca s'affiche en menu deroulant lors de la creation d'un ticket.
public function __toString()
{
$result = $this->vDate . "/" . $this->vHeuredep . "-" . $this->vHeurearv;
return (string) $result;
}
}
Table voyage
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* TrVoyage
*
* @ORM\Table(name="tr_voyage", indexes={@ORM\Index(name="Car_idx", columns={"C_ID"}), @ORM\Index(name="PERSONNEL_idx", columns={"P_ID"}), @ORM\Index(name="DEPARTEMENT_idx", columns={"DLEL_ID"}), @ORM\Index(name="DEPARTEMENT1_idx", columns={"DLEL_ID"}), @ORM\Index(name="DEPARTEMENT2_idx", columns={"DLEL_ID"}), @ORM\Index(name="DEPARTEMENT3_idx", columns={"DLEL_ID"}), @ORM\Index(name="DEPARTEMENT4_idx", columns={"DLEL_ID"}), @ORM\Index(name="DEPARTEMENT5_idx", columns={"DLEL_ID"}), @ORM\Index(name="DEPARTEMENT6_idx", columns={"DLEL_ID"})})
* @ORM\Entity(repositoryClass="App\Repository\TrVoyageRepository")
*/
class TrVoyage
{
/**
* @var int
*
* @ORM\Column(name="V_ID", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $vId;
/**
* @var \DateTime
*
* @ORM\Column(name="V_DATE", type="date", nullable=false)
*/
private $vDate;
/**
* @var \DateTime
*
* @ORM\Column(name="V_HEUREDEP", type="time", nullable=false)
*/
private $vHeuredep;
/**
* @var \DateTime
*
* @ORM\Column(name="V_HEUREARV", type="time", nullable=false)
*/
private $vHeurearv;
/**
* @var \TrDepartement
*
* @ORM\ManyToOne(targetEntity="TrDepartement")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="V_VILLEDEPART", referencedColumnName="DLEL_ID")
* })
*/
private $vVilledepart;
/**
* @var \TrDepartement
*
* @ORM\ManyToOne(targetEntity="TrDepartement")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="V_VILLEARRIVE", referencedColumnName="DLEL_ID")
* })
*/
private $vVillearrive;
/**
* @var \TrDepartement
*
* @ORM\ManyToOne(targetEntity="TrDepartement")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="V_VILLEESCALE1", referencedColumnName="DLEL_ID")
* })
*/
private $vVilleescale1;
/**
* @var \TrDepartement
*
* @ORM\ManyToOne(targetEntity="TrDepartement")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="V_VILLEESCALE2", referencedColumnName="DLEL_ID")
* })
*/
private $vVilleescale2;
/**
* @var \TrDepartement
*
* @ORM\ManyToOne(targetEntity="TrDepartement")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="V_VILLEESCALE3", referencedColumnName="DLEL_ID")
* })
*/
private $vVilleescale3;
/**
* @var \TrDepartement
*
* @ORM\ManyToOne(targetEntity="TrDepartement")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="V_VILLEESCALE4", referencedColumnName="DLEL_ID")
* })
*/
private $vVilleescale4;
/**
* @var \TrDepartement
*
* @ORM\ManyToOne(targetEntity="TrDepartement")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="V_VILLEESCALE5", referencedColumnName="DLEL_ID")
* })
*/
private $vVilleescale5;
/**
* @var int|null
*
* @ORM\Column(name="V_DISTANCE", type="integer", nullable=true)
*/
private $vDistance;
/**
* @var \TrCar
*
* @ORM\ManyToOne(targetEntity="TrCar")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="C_ID", referencedColumnName="C_ID")
* })
*/
private $c;
/**
* @var \TrPersonnel
*
* @ORM\ManyToOne(targetEntity="TrPersonnel")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="P_ID", referencedColumnName="P_ID")
* })
*/
private $p;
public function getVId(): ?int
{
return $this->vId;
}
public function getVDate(): ?\DateTimeInterface
{
return $this->vDate;
}
public function setVDate(\DateTimeInterface $vDate): self
{
$this->vDate = $vDate;
return $this;
}
public function getVHeuredep(): ?\DateTimeInterface
{
return $this->vHeuredep;
}
public function setVHeuredep(\DateTimeInterface $vHeuredep): self
{
$this->vHeuredep = $vHeuredep;
return $this;
}
public function getVHeurearv(): ?\DateTimeInterface
{
return $this->vHeurearv;
}
public function setVHeurearv(\DateTimeInterface $vHeurearv): self
{
$this->vHeurearv = $vHeurearv;
return $this;
}
public function getVVilledepart(): ?TrDepartement
{
return $this->vVilledepart;
}
public function setVVilledepart(?TrDepartement $vVilledepart): self
{
$this->vVilledepart = $vVilledepart;
return $this;
}
public function getVVillearrive(): ?TrDepartement
{
return $this->vVillearrive;
}
public function setVVillearrive(?TrDepartement $vVillearrive): self
{
$this->vVillearrive = $vVillearrive;
return $this;
}
public function getVVilleescale1(): ?TrDepartement
{
return $this->vVilleescale1;
}
public function setVVilleescale1(?TrDepartement $vVilleescale1): self
{
$this->vVilleescale1 = $vVilleescale1;
return $this;
}
public function getVVilleescale2(): ?TrDepartement
{
return $this->vVilleescale2;
}
public function setVVilleescale2(?TrDepartement $vVilleescale2): self
{
$this->vVilleescale2 = $vVilleescale2;
return $this;
}
public function getVVilleescale3(): ?TrDepartement
{
return $this->vVilleescale3;
}
public function setVVilleescale3(?TrDepartement $vVilleescale3): self
{
$this->vVilleescale3 = $vVilleescale3;
return $this;
}
public function getVVilleescale4(): ?TrDepartement
{
return $this->vVilleescale4;
}
public function setVVilleescale4(?TrDepartement $vVilleescale4): self
{
$this->vVilleescale4 = $vVilleescale4;
return $this;
}
public function getVVilleescale5(): ?TrDepartement
{
return $this->vVilleescale5;
}
public function setVVilleescale5(?TrDepartement $vVilleescale5): self
{
$this->vVilleescale5 = $vVilleescale5;
return $this;
}
public function getVDistance(): ?int
{
return $this->vDistance;
}
public function setVDistance(?int $vDistance): self
{
$this->vDistance = $vDistance;
return $this;
}
public function getC(): ?TrCar
{
return $this->c;
}
public function setC(?TrCar $c): self
{
$this->c = $c;
return $this;
}
public function getP(): ?TrPersonnel
{
return $this->p;
}
public function setP(?TrPersonnel $p): self
{
$this->p = $p;
return $this;
}
}
Ce que je veux
Je veux corriger l'erreur afin que je puisse creer un ticket en selection mon voyage en menu deroulant en regardant la date et les villes d'arrivée et de depart.
Ce que j'obtiens
Pitet
Messages postés2826Date d'inscriptionlundi 11 février 2013StatutMembreDernière intervention21 juillet 2022525 13 janv. 2021 à 15:25
Bonjour,
D'après le message d'erreur, la conversion en string est requise pour la classe TrVoyage mais tu as implémenté la fonction __toString dans la classe TrTicket.
Essaye d'implémenter la fonction __toString dans la classe TrVoyage pour corriger l'erreur.
Pitet, merci d'avoir repondu, la fonction __toString() doit être dans la classe TrTicket, afin d'afficher en menu deroulant les voyages . j'ai géré ce genre d'erreur avec les autres tables, seulement avec les tables Trticket et TrVoyage il y'a une difference. je vous explique : quand l'erreur se produit on met la fonction __toString(), chez l'entité fille, en prenant un champ string qui pourra afficher le libellé dans le menu deroulant, mais le cas de la table TrVoyage, il n'a pas de champ string, donc je veux concatener la date et heure de depart pour afficher dans le menu deroulant de l'entité fille qui est TrTicket. si j'avais un champs de type chaine de caractère c'allait etre facile. Donc je cherche à concatener la date et l'heure pour afficher dans le menu deroulant. J'ai essayé ta solution, mais voici la nouvelle erreur : Object of class DateTime could not be converted to string, voici le code ci-dessous :
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* TrVoyage
*
* @ORM\Table(name="tr_voyage", indexes={@ORM\Index(name="Car_idx", columns={"C_ID"}), @ORM\Index(name="PERSONNEL_idx", columns={"P_ID"}), @ORM\Index(name="DEPARTEMENT_idx", columns={"DLEL_ID"}), @ORM\Index(name="DEPARTEMENT1_idx", columns={"DLEL_ID"}), @ORM\Index(name="DEPARTEMENT2_idx", columns={"DLEL_ID"}), @ORM\Index(name="DEPARTEMENT3_idx", columns={"DLEL_ID"}), @ORM\Index(name="DEPARTEMENT4_idx", columns={"DLEL_ID"}), @ORM\Index(name="DEPARTEMENT5_idx", columns={"DLEL_ID"}), @ORM\Index(name="DEPARTEMENT6_idx", columns={"DLEL_ID"})})
* @ORM\Entity(repositoryClass="App\Repository\TrVoyageRepository")
*/
class TrVoyage
{
/**
* @var int
*
* @ORM\Column(name="V_ID", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $vId;
/**
* @var \DateTime
*
* @ORM\Column(name="V_DATE", type="date", nullable=false)
*/
private $vDate;
/**
* @var \DateTime
*
* @ORM\Column(name="V_HEUREDEP", type="time", nullable=false)
*/
private $vHeuredep;
/**
* @var \DateTime
*
* @ORM\Column(name="V_HEUREARV", type="time", nullable=false)
*/
private $vHeurearv;
/**
* @var \TrDepartement
*
* @ORM\ManyToOne(targetEntity="TrDepartement")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="V_VILLEDEPART", referencedColumnName="DLEL_ID")
* })
*/
private $vVilledepart;
/**
* @var \TrDepartement
*
* @ORM\ManyToOne(targetEntity="TrDepartement")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="V_VILLEARRIVE", referencedColumnName="DLEL_ID")
* })
*/
private $vVillearrive;
/**
* @var \TrDepartement
*
* @ORM\ManyToOne(targetEntity="TrDepartement")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="V_VILLEESCALE1", referencedColumnName="DLEL_ID")
* })
*/
private $vVilleescale1;
/**
* @var \TrDepartement
*
* @ORM\ManyToOne(targetEntity="TrDepartement")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="V_VILLEESCALE2", referencedColumnName="DLEL_ID")
* })
*/
private $vVilleescale2;
/**
* @var \TrDepartement
*
* @ORM\ManyToOne(targetEntity="TrDepartement")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="V_VILLEESCALE3", referencedColumnName="DLEL_ID")
* })
*/
private $vVilleescale3;
/**
* @var \TrDepartement
*
* @ORM\ManyToOne(targetEntity="TrDepartement")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="V_VILLEESCALE4", referencedColumnName="DLEL_ID")
* })
*/
private $vVilleescale4;
/**
* @var \TrDepartement
*
* @ORM\ManyToOne(targetEntity="TrDepartement")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="V_VILLEESCALE5", referencedColumnName="DLEL_ID")
* })
*/
private $vVilleescale5;
/**
* @var int|null
*
* @ORM\Column(name="V_DISTANCE", type="integer", nullable=true)
*/
private $vDistance;
/**
* @var \TrCar
*
* @ORM\ManyToOne(targetEntity="TrCar")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="C_ID", referencedColumnName="C_ID")
* })
*/
private $c;
/**
* @var \TrPersonnel
*
* @ORM\ManyToOne(targetEntity="TrPersonnel")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="P_ID", referencedColumnName="P_ID")
* })
*/
private $p;
public function getVId(): ?int
{
return $this->vId;
}
public function getVDate(): ?\DateTimeInterface
{
return $this->vDate;
}
public function setVDate(\DateTimeInterface $vDate): self
{
$this->vDate = $vDate;
return $this;
}
public function getVHeuredep(): ?\DateTimeInterface
{
return $this->vHeuredep;
}
public function setVHeuredep(\DateTimeInterface $vHeuredep): self
{
$this->vHeuredep = $vHeuredep;
return $this;
}
public function getVHeurearv(): ?\DateTimeInterface
{
return $this->vHeurearv;
}
public function setVHeurearv(\DateTimeInterface $vHeurearv): self
{
$this->vHeurearv = $vHeurearv;
return $this;
}
public function getVVilledepart(): ?TrDepartement
{
return $this->vVilledepart;
}
public function setVVilledepart(?TrDepartement $vVilledepart): self
{
$this->vVilledepart = $vVilledepart;
return $this;
}
public function getVVillearrive(): ?TrDepartement
{
return $this->vVillearrive;
}
public function setVVillearrive(?TrDepartement $vVillearrive): self
{
$this->vVillearrive = $vVillearrive;
return $this;
}
public function getVVilleescale1(): ?TrDepartement
{
return $this->vVilleescale1;
}
public function setVVilleescale1(?TrDepartement $vVilleescale1): self
{
$this->vVilleescale1 = $vVilleescale1;
return $this;
}
public function getVVilleescale2(): ?TrDepartement
{
return $this->vVilleescale2;
}
public function setVVilleescale2(?TrDepartement $vVilleescale2): self
{
$this->vVilleescale2 = $vVilleescale2;
return $this;
}
public function getVVilleescale3(): ?TrDepartement
{
return $this->vVilleescale3;
}
public function setVVilleescale3(?TrDepartement $vVilleescale3): self
{
$this->vVilleescale3 = $vVilleescale3;
return $this;
}
public function getVVilleescale4(): ?TrDepartement
{
return $this->vVilleescale4;
}
public function setVVilleescale4(?TrDepartement $vVilleescale4): self
{
$this->vVilleescale4 = $vVilleescale4;
return $this;
}
public function getVVilleescale5(): ?TrDepartement
{
return $this->vVilleescale5;
}
public function setVVilleescale5(?TrDepartement $vVilleescale5): self
{
$this->vVilleescale5 = $vVilleescale5;
return $this;
}
public function getVDistance(): ?int
{
return $this->vDistance;
}
public function setVDistance(?int $vDistance): self
{
$this->vDistance = $vDistance;
return $this;
}
public function getC(): ?TrCar
{
return $this->c;
}
public function setC(?TrCar $c): self
{
$this->c = $c;
return $this;
}
public function getP(): ?TrPersonnel
{
return $this->p;
}
public function setP(?TrPersonnel $p): self
{
$this->p = $p;
return $this;
}
public function __toString()
{
return $this->getVDate() . ' ' . $this->getVHeuredep();
}
}
code TrTicket
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* TrTicket
*
* @ORM\Table(name="tr_ticket", indexes={@ORM\Index(name="VOYAGE_idx", columns={"V_ID"})})
* @ORM\Entity(repositoryClass="App\Repository\TrTicketRepository")
*/
class TrTicket
{
/**
* @var int
*
* @ORM\Column(name="TCK_ID", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $tckId;
/**
* @var string
*
* @ORM\Column(name="TCK_NUM", type="string", length=200, nullable=false)
*/
private $tckNum;
/**
* @var string|null
*
* @ORM\Column(name="TCK_LIB", type="string", length=200, nullable=true)
*/
private $tckLib;
/**
* @var int
*
* @ORM\Column(name="TCK_PRIX", type="integer", nullable=false)
*/
private $tckPrix;
/**
* @var bool|null
*
* @ORM\Column(name="TCK_FLAG", type="boolean", nullable=true, options={"default"="1"})
*/
private $tckFlag = true;
/**
* @var \TrVoyage
*
* @ORM\ManyToOne(targetEntity="TrVoyage")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="V_ID", referencedColumnName="V_ID")
* })
*/
private $v;
public function getTckId(): ?int
{
return $this->tckId;
}
public function getTckNum(): ?string
{
return $this->tckNum;
}
public function setTckNum(string $tckNum): self
{
$this->tckNum = $tckNum;
return $this;
}
public function getTckLib(): ?string
{
return $this->tckLib;
}
public function setTckLib(?string $tckLib): self
{
$this->tckLib = $tckLib;
return $this;
}
public function getTckPrix(): ?int
{
return $this->tckPrix;
}
public function setTckPrix(int $tckPrix): self
{
$this->tckPrix = $tckPrix;
return $this;
}
public function getTckFlag(): ?bool
{
return $this->tckFlag;
}
public function setTckFlag(?bool $tckFlag): self
{
$this->tckFlag = $tckFlag;
return $this;
}
public function getV(): ?TrVoyage
{
return $this->v;
}
public function setV(?TrVoyage $v): self
{
$this->v = $v;
return $this;
}
}
14 janv. 2021 à 11:48
code TrTicket