i'm new in symfony and i try to display an image in a view with Vich Uploader but don't know how to. I have an entity Brand with a logo related (OneToMany) to an entity Ship. When i try to display the logo of the brand in the view (show.html.twig) of Ship the logo is not displayed, I've no error in console/symfony and twig part with the img tag don't appear in the inspector.
I can diplay all my logo by a findAll() but can't find a way to only display the brand logo of the ship selected.
Any idea?
Ship entity :
<?php
namespace App\Entity;
use App\Repository\ShipRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\HttpFoundation\File\File;
/**
* @ORM\Entity(repositoryClass=ShipRepository::class)
* @Vich\Uploadable
*/
class Ship
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\ManyToOne(targetEntity=Brand::class, inversedBy="ships")
*/
private $brand;
Brand entity :
<?php
namespace App\Entity;
use App\Repository\BrandRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\HttpFoundation\File\File;
/**
* @ORM\Entity(repositoryClass=BrandRepository::class)
* @Vich\Uploadable
*/
class Brand
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\OneToMany(targetEntity=Ship::class, mappedBy="brand")
*/
private $ships;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $logo;
/**
* @Vich\UploadableField(mapping="brand_logo", fileNameProperty="logo")
* @var File
*/
private $logoFile;
public function getLogo(): ?string
{
return $this->logo;
}
public function setLogo(?string $logo): self
{
$this->logo = $logo;
return $this;
}
public function getLogoFile()
{
return $this->logoFile;
}
public function setLogoFile(File $logo = null)
{
$this->logoFile = $logo;
// VERY IMPORTANT:
// It is required that at least one field changes if you are using Doctrine,
// otherwise the event listeners won't be calles and the file is lost
if ($logo) {
// if 'updatedAt' is not defined in your entity, use another property
$this->createdAt = new \DateTime('now');
}
}
ShipController.php :
<?php
namespace App\Controller\Front;
use App\Data\SearchData;
use App\Entity\Ship;
use App\Entity\Brand;
use App\Entity\ShipImages;
use App\Form\SearchType;
use App\Repository\ShipRepository;
use App\Repository\BrandRepository;
use App\Repository\ShipImagesRepository;
use App\Form\ShipType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/ship")
*/
class ShipController extends AbstractController
{
/**
* @Route("/", name="admin_ship_index", methods={"GET"})
*/
public function index(ShipRepository $shipRepository, Request $request): Response
{
$data = new SearchData();
$data->page = $request->get('page', 1);
$form = $this->createForm(SearchType::class, $data);
$form->handleRequest($request);
$ships = $shipRepository->findSearch($data);
if($request->isXmlHttpRequest()) {
return new JsonResponse([
'content' => $this->renderView('main/_ship.html.twig', ['ship_list' => $ships]),
'pagination' => $this->renderView('main/_pagination.html.twig', ['ship_list' => $ships]),
]);
}
return $this->render('admin/ship/index.html.twig', [
'ship_list' => $ships,
'form' => $form->createView()
]);
}
/**
* @Route("/show/{id}", name="admin_ship_show", methods={"GET"})
*/
public function shipShow(Ship $ship): Response
{
$ship->getBrand()->getLogoFile();
//dd($ship);
return $this->render('admin/ship/show.html.twig', [
'ship' => $ship,
]);
}
}
Show.html.twig :
{% extends 'base.html.twig' %}
{% block title %}shipName{% endblock %}
{% block body %}
<div class="main-content">
<h2>{{ ship.name }}</h2>
<div class="second-wrapper">
<div class="image-holder">
<img src="{{ vich_uploader_asset(ship, 'shipViewFile') }}"/>
</div>
</div>
<div class="first-wrapper">
<div class="ship-model">
<div class="model-block-content">
<h4>Type</h4>
<p>{{ ship.type }}</p>
</div>
<div class="model-block-content">
<h4>Manufacturer</h4>
<p>{{ ship.brand }}</p>
{% for brand in ship %}
<img src="{{ vich_uploader_asset(brand, 'logo') }}"/>
{% endfor %}
</div>