Bonjour,
Voila je rencontre un petit problème avec mon code.
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;
}
}
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.
j'ai fait ca :
public function __toString()
{
$result = $this->vDate . "/" . $this->vHeuredep . "-" . $this->vHeurearv;
return (string) $result;
}
mais l'erreur reste inchangée.
Svp aidez moi a gerer cette erreur.
Bonjour,
$this->vDate est un Object (donc un objet), item pour vHeuredep et vHeurearv.
Je pense que tu pourrais essayer de le convertir en string en essayant le code ci-dessous :
$result = $this->vDate->format('d/m/Y') . "/" . $this->vHeuredep->format('H:i:s') . "-" . $this->vHeurearv->format('H:i:s');
// format('d m Y') convertit en string la date (vDate) en jour/mois/année
// format('H:i:s') convertit en string la date (vHeuredep / vHeurearv) en heure:min:seconde
Tu peux essayer de t'aider de la doc PHP ici : https://www.php.net/manual/fr/datetime.format.php