Hi guys this is my error :
Object of class Proxies\__CG__\App\Entity\Mark could not be converted to string
and this error twig:
this is my controller call and my tables mark and car
controller:
class ShowRoomController extends AbstractController
{
/**
* Permet d'afficher l'ensemble des voitures du showrooms
* @Route("/shows", name="show")
*/
public function index( VoitureRepository $repo): Response
{
$shows = $repo->findAll();
return $this->render('show_room/index.html.twig', [
'shows' => $shows
]);
}
}
this my entity car:
/**
* @ORM\ManyToOne(targetEntity=Mark::class, inversedBy="voiture")
* @ORM\JoinColumn(nullable=false)
*/
private $mark;
and my entity mark
/**
* @ORM\OneToMany(targetEntity=Voiture::class, mappedBy="mark", orphanRemoval=true)
*/
private $voiture;
this my code typed in twig
{% for show in shows %}
{#{% set url = path('ads_show', {'slug':ad.slug}) %}#}
<div class="col-md-4">
<div class="card bg-light mb-3">
<div class="card-img">
<img class="img-fluid"
src="images/cars/{{show.coverImage }}" alt="test">
<div class="try">
{{show.mark}}
</div>
where is the error?
sorry for my basic english. I speak French at the base and thank you for your help.
CodePudding user response:
the error message is clear, the system could not convert an object of type Mark to string, so the solution is to use a property of the object $mark or implement the method __toString in the Mark class.
{{ show.mark.name }}
for example.
OR
class Mark
{
...
public function __toString()
{
return $this->name;
}
...
}
CodePudding user response:
you cant do this => {{show.mark}}
mark is object is not a string
if you want to show the title of a Mark for exemple you need to do this =>{{show.mark.title}}
if your mark entity has title