Post data contains list attribute in symfony - php

I have a manyToMany association between Tweet and Hashtag. I am trying to submit the Tweet form to create a tweet with the selected hashtags from the checked boxs. In addition, i'm using FosRestBundle.
Is there any solution?
I tried to do like this but data inserted but hashtags never !
Tweet Entity:
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="App\Repository\TweetRepository")
*/
class Tweet
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="date")
*/
private $PublishDate;
/**
* #ORM\ManyToMany(targetEntity="App\Entity\Hashtag", inversedBy="tweets")
*/
private $hashtags;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\TwitterProfile", inversedBy="tweets")
*/
private $author;
public function __construct()
{
$this->hashtags = new ArrayCollection();
$this->twitterProfiles = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getPublishDate(): ?\DateTimeInterface
{
return $this->PublishDate;
}
public function setPublishDate(\DateTimeInterface $PublishDate): self
{
$this->PublishDate = $PublishDate;
return $this;
}
/**
* #return Collection|Hashtag[]
*/
public function getHashtags(): Collection
{
return $this->hashtags;
}
public function addHashtag(Hashtag $hashtag): self
{
if (!$this->hashtags->contains($hashtag)) {
$this->hashtags[] = $hashtag;
}
return $this;
}
public function removeHashtag(Hashtag $hashtag): self
{
if ($this->hashtags->contains($hashtag)) {
$this->hashtags->removeElement($hashtag);
}
return $this;
}
public function getAuthor(): ?TwitterProfile
{
return $this->author;
}
public function setAuthor(?TwitterProfile $author): self
{
$this->author = $author;
return $this;
}
}
TweetType:
<?php
namespace App\Forms;
use App\Entity\Tweet;
use App\Entity\TwitterProfile;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class TweetType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('PublishDate', DateType::class,[
'widget' => 'choice'
])
->add('hashtags', CollectionType::class,[
'entry_type' => HashtagType::class
])
->add('author', EntityType::class,[
'class' => TwitterProfile::class,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('tp');
}
])
->add('save', SubmitType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class'
=>
Tweet::class,
'csrf_protection'
=>
false
));
}
}
TweetController:
<?php
namespace App\Controller;
use App\Entity\Tweet;
use App\Forms\TweetType;
use Doctrine\Common\Collections\ArrayCollection;
use FOS\RestBundle\Controller\FOSRestController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use FOS\RestBundle\Controller\Annotations as Rest;
class TweetController extends FOSRestController
{
/**
* List All Tweets
* #Rest\Get("/getAllTweets")
* #return Response
*/
public function getTweets()
{
$repository=$this->getDoctrine()->getRepository(Tweet::class);
$tweets=$repository->findall();
return$this->handleView($this->view($tweets));
}
/**
* List One Tweet
* #Rest\Get("/tweet/{id}")
* #param int $id
* #return Response
*/
public function getOneTweet(int $id)
{
$repository=$this->getDoctrine()->getRepository(Tweet::class);
$tweet=$repository->find($id);
if ($tweet == null){
throw new NotFoundHttpException("this tweet with id:".$id." not founded");
}
return $this->handleView($this->view($tweet));
}
/**
* List One Hashtag to recommand
* #Rest\Get("/tweets/recommanded-hashtag/group/{groupId}")
* #param int $groupId
* #return Response
*/
public function getHashtagToRecommand(int $groupId)
{
$hashs = new ArrayCollection();
$repository=$this->getDoctrine()->getRepository(Tweet::class);
$tweets = $repository->findHashtagToRecommand($groupId);
if ($tweets == null){
throw new NotFoundHttpException("this hashtag not founded");
}
foreach ($tweets as $key){
$hashs->add($key->getHashtags());
}
return $this->handleView($this->view($hashs));
}
/**
* Create New Tweet
* #Rest\Post("/tweet/add")
*
* #return Response
*/
public function postTweet(Request $request)
{
$tweet = new Tweet();
$form = $this->createForm(TweetType::class,$tweet);
$data = json_decode($request->getContent(),true);
$form->submit($data);
if ($form->isSubmitted() && $form->isValid()){
$em = $this->getDoctrine()->getManager();
$em->persist($tweet);
$em->flush();
return $this->handleView($this->view(['status' => 'ok'], Response::HTTP_CREATED));
}
return $this->handleView($this->view($form->getErrors()));
}
/**
* Update Tweet
* #Rest\Put("/tweet/update/{id}")
* #param int $id
* #return Response
*/
public function putTweet(Request $request, int $id)
{
$repository=$this->getDoctrine()->getRepository(Tweet::class);
$existingTweet = $repository->find($id);
if (null === $existingTweet) {
throw new NotFoundHttpException();
}
$form = $this->createForm(TweetType::class, $existingTweet);
$form->submit($request->request->all());
if (false === $form->isValid()) {
return $this->handleView($this->view($form->getErrors()));
}
$em = $this->getDoctrine()->getManager();
$em->flush();
return $this->handleView($this->view(['status' => 'ok'], Response::HTTP_OK));
}
/**
* Update Partially Tweet
* #Rest\Patch("/tweet/patch/{id}")
* #param int $id
* #return Response
*/
public function patchTweet(Request $request, int $id)
{
$repository=$this->getDoctrine()->getRepository(Tweet::class);
$existingTweet = $repository->find($id);
if (null === $existingTweet) {
throw new NotFoundHttpException();
}
$form = $this->createForm(TweetType::class, $existingTweet);
$form->submit($request->request->all());
if (false === $form->isValid()) {
return $this->handleView($this->view($form->getErrors()));
}
$em = $this->getDoctrine()->getManager();
$em->flush();
return $this->handleView($this->view(['status' => 'ok'], Response::HTTP_OK));
}
/**
* Delete Tweet
* #Rest\Delete("/tweet/{id}")
* #param int $id
* #return Response
*/
public function deleteTweet(int $id)
{
$repository=$this->getDoctrine()->getRepository(Tweet::class);
$existingTweet = $repository->find($id);
$em = $this->getDoctrine()->getManager();
$em->remove($existingTweet);
$em->flush();
return $this->handleView($this->view(['status' => 'ok'], Response::HTTP_NO_CONTENT));
}
}

Related

Bug Symfony Can use "yield from" only with arrays

I'm creating a website with Symfony 5. I have a problem that I can't solve.
I need to be able to create entities and for that I want to use a form generated by Symfony.
Symfony sends me this error:
Can use "yield from" only with arrays and Traversables
If I modify anything in my "Entities" entity then symfony sends me back:
My field I was waiting for
Great, that's exactly what I need, but the problem is that if I refresh the page the error comes back.
<?php
namespace App\Controller\Admin;
use App\Entity\Entities;
use App\Entity\Events;
use App\Form\EntitiesType;
use App\Form\EventsType;
use App\Repository\CriteriasRepository;
use App\Repository\EntitiesRepository;
use App\Repository\EventsRepository;
use Doctrine\Persistence\ObjectManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
Class AdminHome extends AbstractController {
protected $event;
protected $entity;
protected $criteria;
protected $twig;
public function __construct(EventsRepository $event, EntitiesRepository $entity, CriteriasRepository $criteria, ObjectManager $em)
{
$this->event = $event;
$this->entity = $entity;
$this->criteria = $criteria;
$this->em = $em;
}
public function index(){
$event = $this->event->findOneBy(['current' => 1]);
$criterias = $this->indexCriteria($event);
$entities = $this->indexEntity($event);
return new Response($this->render('admin/home.html',[ 'event' => $event, 'entities' => $entities, 'criterias' => $criterias ]));
}
public function indexEntity(Events $event){
return $event->getEntity();
}
public function indexCriteria(Events $event){
return $event->getCriterias();
}
/**
* #Route("/admin/event/create", name="admin.event.new")
*/
public function newEvent(Request $request){
$event = new Events();
$form = $this->createForm(EventsType::class, $event);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
$this->em->persist($event);
$this->em->flush();
return $this->redirectToRoute('adminHome');
}
return $this->render('admin/event/eventNew.html', ['event' => $event, 'form' => $form->createView()]);
}
/**
* #Route("/admin/entity/create", name="admin.entity.new")
*/
public function newEntity(Request $request){
$entity = new Entities();
$form = $this->createForm(EntitiesType::class, $entity);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
$this->em->persist($entity);
$this->em->flush();
return $this->redirectToRoute('adminHome');
}
return $this->render('admin/entity/entityNew.html', ['entity' => $entity, 'form' => $form->createView()]);
}
// /**
// * #Route("/admin/entity/edit", name="admin.entity.edit")
// */
// public function editEntity(Entities $entity, Request $request){
// $form = $this->createForm(EntityType::class, $entity);
// $form->handleRequest($request);
// if($form->isSubmitted() && $form->isValid()){
// $this->em->flush();
// return $this->redirectToRoute('adminHome');
// }
// return $this->render('admin/entityEdit.html', ['entity' => $entity, 'form' => $form->createView()]);
// }
}
?>
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass=EntitiesRepository::class)
*/
class Entities
{
/**
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $entityName;
/**
* #ORM\OneToMany(targetEntity=Rates::class, mappedBy="entity")
*/
private $rates;
/**
* #ORM\ManyToMany(targetEntity=Events::class, mappedBy="entity")
*/
private $event;
/**
* #ORM\Column(type="string", length=255)
*/
private $descriptionFr;
/**
* #ORM\Column(type="string", length=255, nullable=true)
*/
private $descriptionEn;
/**
* #ORM\ManyToMany(targetEntity=Themes::class, inversedBy="entities")
*/
private $theme;
public function __construct()
{
$this->rates = new ArrayCollection();
$this->event = new ArrayCollection();
$this->theme = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getEntityName(): ?string
{
return $this->entityName;
}
public function setEntityName(string $entityName): self
{
$this->entityName = $entityName;
return $this;
}
/**
* #return Collection|Rates[]
*/
public function getRates(): Collection
{
return $this->rates;
}
public function addRate(Rates $rate): self
{
if (!$this->rates->contains($rate)) {
$this->rates[] = $rate;
$rate->setEntity($this);
}
return $this;
}
public function removeRate(Rates $rate): self
{
if ($this->rates->removeElement($rate)) {
// set the owning side to null (unless already changed)
if ($rate->getEntity() === $this) {
$rate->setEntity(null);
}
}
return $this;
}
/**
* #return Collection|Events[]
*/
public function getEvent(): Collection
{
return $this->event;
}
public function addEvent(Events $event): self
{
if (!$this->event->contains($event)) {
$this->event[] = $event;
$event->addEntity($this);
}
return $this;
}
public function removeEvent(Events $event): self
{
if ($this->event->removeElement($event)) {
$event->removeEntity($this);
}
return $this;
}
public function getDescriptionFr(): ?string
{
return $this->descriptionFr;
}
public function setDescriptionFr(string $descriptionFr): self
{
$this->descriptionFr = $descriptionFr;
return $this;
}
public function getDescriptionEn(): ?string
{
return $this->descriptionEn;
}
public function setDescriptionEn(?string $descriptionEn): self
{
$this->descriptionEn = $descriptionEn;
return $this;
}
/**
* #return Collection|Themes[]
*/
public function getTheme(): Collection
{
return $this->theme;
}
public function addTheme(Themes $theme): self
{
if (!$this->theme->contains($theme)) {
$this->theme[] = $theme;
}
return $this;
}
public function removeTheme(Themes $theme): self
{
$this->theme->removeElement($theme);
return $this;
}
}
<?php
namespace App\Form;
use App\Entity\Entities;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class EntitiesType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('entityName')
->add('descriptionFr' )
->add('descriptionEn')
->add('event')
->add('theme')
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Entities::class,
]);
}
}
{{ form_start(form) }}
{{ form_rest(form) }}
<button class="btn">{{ button|default('Créer')}}</button>
{{ form_end(form) }}

How to fix "but the new value must be an array or an instance of \Traversable" error in Symfony

I build a management app of bagages and voyages with Symfony 4, I created a "ManyToMany" relation between this entities.
When I want to add a new bagage with a destination (voyage) I have this error :
I have addVoyage and removeVoyage in my classes Voyage and Bagage.
You will find above my classes Bagage.php, my form BagageType.php and my controller BagageController.php
Bagage.php
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity(repositoryClass="App\Repository\BagageRepository")
*/
class Bagage
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string")
* #Assert\Length(min=5, max=50)
*/
private $nom;
/**
* #ORM\Column(type="json", nullable=true)
*/
private $objets = [];
/**
* #ORM\Column(type="datetime")
*/
private $dateCreation;
/**
* #ORM\ManyToMany(targetEntity="App\Entity\Voyage", mappedBy="bagages")
*/
private $voyages;
public function __construct()
{
$this->voyages = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getNom(): ?string
{
return $this->nom;
}
public function setNom(string $nom): self
{
$this->nom = $nom;
return $this;
}
public function getObjets(): ?array
{
return $this->objets;
}
public function setObjets(?array $objets): self
{
$this->objets = $objets;
return $this;
}
public function getDateCreation(): ?\DateTimeInterface
{
return $this->dateCreation;
}
public function setDateCreation(\DateTimeInterface $dateCreation): self
{
$this->dateCreation = $dateCreation;
return $this;
}
/**
* #return Collection|Voyage[]
*/
public function getVoyages(): Collection
{
return $this->voyages;
}
public function addVoyage(Voyage $voyage): self
{
if (!$this->voyages->contains($voyage)) {
$this->voyages[] = $voyage;
$voyage->addBagage($this);
}
return $this;
}
public function removeVoyage(Voyage $voyage): self
{
if ($this->voyages->contains($voyage)) {
$this->voyages->removeElement($voyage);
$voyage->removeBagage($this);
}
return $this;
}
}
?>
BagageType.php
<?php
namespace App\Form;
use App\Entity\Voyage;
use App\Entity\Bagage;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class BagageType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('nom')
->add('voyages', EntityType::class, [
'class' => Voyage::class,
'choice_label' => 'lieu'
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Bagage::class,
]);
}
}
?>
BagageController.php
<?php
/**
* #Route("/bagages/nouveau", name="bagage_creation")
* #Route("/bagages/{id}/edit", name="bagage_edit")
*/
public function form(Bagage $bagage = null, Request $request, ObjectManager $manager)
{
if(!$bagage){
$bagage = new Bagage();
}
$form = $this->createForm(BagageType::class, $bagage);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()) {
if(!$bagage->getId()){
$bagage->setDateCreation(new \Datetime());
}
$manager->persist($bagage);
$manager->flush();
return $this->redirectToRoute('bagage_show', [
'id' => $bagage->getId()
]);
}
return $this->render('bagages/create.html.twig', [
//creation d'une vue pour twig pour afficher le formulaire
'formBagage' => $form->createView(),
'editMode' => $bagage->getId() !== null
]);
}
?>
Does it miss an addVoyage call in my controller ?
EDIT
Voyage.php
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity(repositoryClass="App\Repository\VoyageRepository")
*/
class Voyage
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $lieu;
/**
* #ORM\ManyToMany(targetEntity="App\Entity\Bagage", inversedBy="voyages")
*/
private $bagages;
public function __construct()
{
$this->bagages = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getLieu(): ?string
{
return $this->lieu;
}
public function setLieu(string $lieu): self
{
$this->lieu = $lieu;
return $this;
}
/**
* #return Collection|Bagage[]
*/
public function getBagages(): Collection
{
return $this->bagages;
}
public function addBagage(Bagage $bagage): self
{
if (!$this->bagages->contains($bagage)) {
$this->bagages[] = $bagage;
}
return $this;
}
public function removeBagage(Bagage $bagage): self
{
if ($this->bagages->contains($bagage)) {
$this->bagages->removeElement($bagage);
}
return $this;
}
}
It looks like you should set multiple attribute to true, making the field's value an array instead of a single entity.
->add('voyages', EntityType::class, [
'class' => Voyage::class,
'choice_label' => 'lieu',
'multiple' => true,
]);

Serialization of 'Symfony\Component\HttpFoundation\File\File' is not allowed, Symfony4?

I've added an avatar image to my User class. When I wanted to render my edit form, I got this error
Serialization of 'Symfony\Component\HttpFoundation\File\File' is not allowed
I tried to solve the problem by implementing \Serializable in my User class according to Symfony Official Documentation. but didint work.
Here is my UserEntity, Artsite.php:
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use
Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
use Serializable;
/**
*
#ORM\Entity(repositoryClass="App\Repository\ArtisteRepository")
* #UniqueEntity(fields={"username"}, message="There is already
an account with this username")
*/
class Artiste implements UserInterface, Serializable
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $NomPrenom;
/**
* #ORM\Column(type="string", length=255)
*/
private $adresse;
/**
* #ORM\Column(type="integer")
*/
private $numero1;
/**
* #ORM\Column(type="integer", nullable=true)
*/
private $numero2;
/**
* #ORM\Column(type="string", length=255)
*/
private $username;
/**
* #ORM\Column(type="string", length=255)
*/
private $password;
/**
* #ORM\Column(type="string", nullable=true)
* #Assert\File(mimeTypes={ "image/*" },mimeTypesMessage="C'est n'est pas une image")
*/
private $image;
/**
* #ORM\OneToMany(targetEntity="App\Entity\Produit", mappedBy="artiste_id")
*/
private $produits;
public function __construct()
{
$this->produits = new ArrayCollection();
}
public function getImage()
{
return $this->image;
}
public function setImage($image)
{
$this->image = $image;
return $this;
}
public function getId(): ?int
{
return $this->id;
}
public function getNomPrenom(): ?string
{
return $this->NomPrenom;
}
public function setNomPrenom(string $NomPrenom): self
{
$this->NomPrenom = $NomPrenom;
return $this;
}
public function getAdresse(): ?string
{
return $this->adresse;
}
public function setAdresse(string $adresse): self
{
$this->adresse = $adresse;
return $this;
}
public function getNumero1(): ?int
{
return $this->numero1;
}
public function setNumero1(int $numero1): self
{
$this->numero1 = $numero1;
return $this;
}
public function getNumero2(): ?int
{
return $this->numero2;
}
public function setNumero2(?int $numero2): self
{
$this->numero2 = $numero2;
return $this;
}
public function getUsername(): ?string
{
return $this->username;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* #return Collection|Produit[]
*/
public function getProduits(): Collection
{
return $this->produits;
}
public function addProduit(Produit $produit): self
{
if (!$this->produits->contains($produit)) {
$this->produits[] = $produit;
$produit->setArtisteId($this);
}
return $this;
}
public function removeProduit(Produit $produit): self
{
if ($this->produits->contains($produit)) {
$this->produits->removeElement($produit);
// set the owning side to null (unless already changed)
if ($produit->getArtisteId() === $this) {
$produit->setArtisteId(null);
}
}
return $this;
}
public function eraseCredentials()
{
}
public function getSalt()
{
}
public function getRoles(): array
{
$roles[] = 'ROLE_Etudiant';
return array_unique($roles);
}
/** #see \Serializable::serialize() */
public function serialize()
{
return serialize(array(
$this->id,
$this->image,
$this->username,
$this->password,
));
}
/** #see \Serializable::unserialize() */
public function unserialize($serialized)
{
list (
$this->id,
$this->image,
$this->username,
$this->password,
) = unserialize($serialized, array('allowed_classes' => false));
}
}
Here is my UserController.php
<?php
namespace App\Controller;
use App\Entity\Artiste;
use App\Form\ArtisteType;
use App\Repository\ArtisteRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use
Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use
Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\File\File;
class ArtisteController extends AbstractController
{
/**
* #Route("/", name="artiste_index", methods={"GET"})
*/
public function index(ArtisteRepository $artisteRepository,Security $security)
{
$user = $security->getUser();
return $this->render('artiste/index.html.twig', [
'client' =>$user
]);
}
/**
* #Route("/login", name="artiste_login", methods={"GET","POST"})
*/
public function login(AuthenticationUtils $authenticationUtils)
{
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('artiste/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
]);
}
/**
* #Route("/logout", name="artiste_logout")
*/
public function logout()
{
}
/**
* #Route("/new", name="artiste_new", methods={"GET","POST"})
*/
public function new(Request $request,UserPasswordEncoderInterface $encoder): Response
{
$artiste = new Artiste();
$form = $this->createForm(ArtisteType::class, $artiste);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$hash=$encoder->encodePassword($artiste,$artiste->getPassword());
$artiste->setPassword($hash);
$file = $artiste->getImage();
if($file=="")
$artiste->setImage("default.jpg");
else{
$fileName = $this->generateUniqueFileName().'.'.$file->guessExtension();
// Move the file to the directory where brochures are stored
try {
$file->move(
$this->getParameter('images_directory'),
$fileName
);
} catch (FileException $e) {
// ... handle exception if something happens during file upload
}
$artiste->setImage($fileName);
}
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($artiste);
$entityManager->flush();
return $this->redirectToRoute('artiste_login');
}
return $this->render('artiste/new.html.twig', [
'artiste' => $artiste,
'form' => $form->createView(),
]);
}
/**
* #return string
*/
private function generateUniqueFileName()
{
// md5() reduces the similarity of the file names generated by
// uniqid(), which is based on timestamps
return md5(uniqid());
}
/**
* #Route("/profile", name="artiste_profile", methods={"GET"})
*/
public function show(Security $security)
{
$user = $security->getUser();
return $this->render('artiste/profile.html.twig', [
'client' => $user,
]);
}
/**
* #Route("/edit", name="artiste_edit", methods={"GET","POST"})
*/
public function edit(Request $request,Security $security,UserPasswordEncoderInterface $encoder)
{
$artiste=$security->getUser();
$artiste->setImage( new File($this->getParameter('images_directory').'/'.$artiste->getImage()));
$form = $this->createForm(ArtisteType::class, $artiste);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$hash=$encoder->encodePassword($artiste,$artiste->getPassword());
$artiste->setPassword($hash);
$file = $artiste->getImage();
$fileName = $this->generateUniqueFileName().'.'.$file->guessExtension();
// Move the file to the directory where brochures are stored
try {
$file->move(
$this->getParameter('images_directory'),
$fileName
);
} catch (FileException $e) {
// ... handle exception if something happens during file upload
}
$artiste->setImage($fileName);
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('artiste_profile', [
'client' => $artiste,
]);
}
return $this->render('artiste/edit.html.twig', [
'client' => $artiste,
'form' => $form->createView(),
]);
}
Here is my UserForm, UserType.php
<?php
namespace App\Form;
use App\Entity\Artiste;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
class ArtisteType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array
$options)
{
$builder
->add('NomPrenom')
->add('adresse')
->add('image', FileType::class, array('required' => false))
->add('numero1')
->add('numero2')
->add('username')
->add('password',PasswordType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Artiste::class,
]);
}
}

exist and have public access in class "Symfony\Component\Form\FormView". Symfony 4

I've a problem about datas on my form view
The principle is the same as this video (https://www.youtube.com/watch?v=MRfsHix1eRA&t=1257s), that is to say: when I select a "management" I want the "services" associated to this "management".
These must be selected in a "report Form" (Report Entity).
OneOrMany service is linked to One management, and a report is linked to a management and a service.
Entities
Management.php
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="App\Repository\ManagementRepository")
*/
class Management
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="string", length=255)
*/
private $name;
/**
* #ORM\OneToMany(targetEntity="App\Entity\Service", mappedBy="management")
*/
private $services;
/**
* #ORM\OneToMany(targetEntity="App\Entity\Report", mappedBy="management")
*/
private $reports;
public function __construct()
{
$this->services = new ArrayCollection();
$this->reports = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
/**
* #return Collection|Service[]
*/
public function getServices(): Collection
{
return $this->services;
}
public function addService(Service $service): self
{
if (!$this->services->contains($service)) {
$this->services[] = $service;
$service->setManagement($this);
}
return $this;
}
public function removeService(Service $service): self
{
if ($this->services->contains($service)) {
$this->services->removeElement($service);
// set the owning side to null (unless already changed)
if ($service->getManagement() === $this) {
$service->setManagement(null);
}
}
return $this;
}
/**
* #return Collection|Report[]
*/
public function getReports(): Collection
{
return $this->reports;
}
public function addReport(Report $report): self
{
if (!$this->reports->contains($report)) {
$this->reports[] = $report;
$report->setManagement($this);
}
return $this;
}
public function removeReport(Report $report): self
{
if ($this->reports->contains($report)) {
$this->reports->removeElement($report);
// set the owning side to null (unless already changed)
if ($report->getManagement() === $this) {
$report->setManagement(null);
}
}
return $this;
}
}
Service.php
<?php
namespace App\Entity;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="App\Repository\ReportRepository")
*/
class Report
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="date")
*/
private $assigned_date;
/**
* #ORM\Column(type="decimal", precision=7, scale=2, nullable=true)
*/
private $time;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Game", inversedBy="reports")
*/
private $game;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Management", inversedBy="reports")
*/
private $management;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Service", inversedBy="reports")
*/
private $service;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Format", inversedBy="reports")
*/
private $format;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="reports")
* #ORM\JoinColumn(nullable=false)
*/
private $category;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="reports")
* #ORM\JoinColumn(nullable=false)
*/
private $user;
public function getId(): ?int
{
return $this->id;
}
public function getAssignedDate(): ?\DateTimeInterface
{
return $this->assigned_date;
}
public function setAssignedDate(\DateTimeInterface $assigned_date): self
{
$this->assigned_date = $assigned_date;
return $this;
}
public function getTime()
{
return $this->time;
}
public function setTime($time): self
{
$this->time = $time;
return $this;
}
public function getGame(): ?Game
{
return $this->game;
}
public function setGame(?Game $game): self
{
$this->game = $game;
return $this;
}
public function getManagement(): ?Management
{
return $this->management;
}
public function setManagement(?Management $management): self
{
$this->management = $management;
return $this;
}
public function getService(): ?Service
{
return $this->service;
}
public function setService(?Service $service): self
{
$this->service = $service;
return $this;
}
public function getFormat(): ?Format
{
return $this->format;
}
public function setFormat(?Format $format): self
{
$this->format = $format;
return $this;
}
public function getCategory(): ?Category
{
return $this->category;
}
public function setCategory(?Category $category): self
{
$this->category = $category;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
}
Report.php
<?php
namespace App\Entity;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="App\Repository\ReportRepository")
*/
class Report
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\Column(type="date")
*/
private $assigned_date;
/**
* #ORM\Column(type="decimal", precision=7, scale=2, nullable=true)
*/
private $time;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Game", inversedBy="reports")
*/
private $game;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Management", inversedBy="reports")
*/
private $management;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Service", inversedBy="reports")
*/
private $service;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Format", inversedBy="reports")
*/
private $format;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="reports")
* #ORM\JoinColumn(nullable=false)
*/
private $category;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="reports")
* #ORM\JoinColumn(nullable=false)
*/
private $user;
public function getId(): ?int
{
return $this->id;
}
public function getAssignedDate(): ?\DateTimeInterface
{
return $this->assigned_date;
}
public function setAssignedDate(\DateTimeInterface $assigned_date): self
{
$this->assigned_date = $assigned_date;
return $this;
}
public function getTime()
{
return $this->time;
}
public function setTime($time): self
{
$this->time = $time;
return $this;
}
public function getGame(): ?Game
{
return $this->game;
}
public function setGame(?Game $game): self
{
$this->game = $game;
return $this;
}
public function getManagement(): ?Management
{
return $this->management;
}
public function setManagement(?Management $management): self
{
$this->management = $management;
return $this;
}
public function getService(): ?Service
{
return $this->service;
}
public function setService(?Service $service): self
{
$this->service = $service;
return $this;
}
public function getFormat(): ?Format
{
return $this->format;
}
public function setFormat(?Format $format): self
{
$this->format = $format;
return $this;
}
public function getCategory(): ?Category
{
return $this->category;
}
public function setCategory(?Category $category): self
{
$this->category = $category;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
}
FormType
ReportType.php
<?php
namespace App\Form;
use App\Entity\Report;
use App\Entity\Game;
use App\Entity\Management;
use App\Entity\Service;
use App\Entity\Format;
use App\Entity\Category;
use App\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
class ReportType extends AbstractType
{
protected $tokenStorage;
public function __construct(TokenStorage $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$user = $this->tokenStorage->getToken()->getUser();
$builder->add('assigned_date', DateType::class, array(
'widget' => 'single_text',
// prevents rendering it as type="date", to avoid HTML5 date pickers
'html5' => false,
// adds a class that can be selected in JavaScript
'attr' => ['class' => 'js-datepicker'],
));
$builder->add('time');
$builder->add('game', EntityType::class, array(
'class' => Game::class,
'placeholder' => '',
'choice_label' => 'name'));
$builder->add('management', EntityType::class, array(
'class' => Management::class,
'placeholder' => 'Select a management',
'mapped' => false,
'choice_label' => 'name'));
$builder->get('management')->addEventListener(
FormEvents::POST_SUBMIT,
function (FormEvent $event)
{
$form = $event->getForm();
$form->getParent()->add('service',EntityType::class, array(
'class' => Service::class,
'placeholder' => 'Select a service',
'choices' => $form->getData()->getServices()
));
}
);
$builder->add('format', EntityType::class, array(
'class' => Format::class,
'placeholder' => '',
'choice_label' => 'name'));
$builder->add('category', EntityType::class, array(
'class' => Category::class,
'placeholder' => '',
'choice_label' => 'name'));
$builder->add('user', EntityType::class, array(
'class' => User::class,
'query_builder' => function (EntityRepository $er) {
$user = $this->tokenStorage->getToken()->getUser();
return $er->createQueryBuilder('u')
->Where('u.id='. $user->getId());
},
'choice_label' => 'id',
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Report::class,
]);
}
}
Twig associated
_form.twig.php
<?php
namespace App\Controller;
use App\Entity\Report;
use App\Form\ReportType;
use App\Repository\ReportRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* #Route("/report")
*/
class ReportController extends AbstractController
{
/**
* #Route("/", name="report_index", methods="GET")
*/
public function index(ReportRepository $reportRepository): Response
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$user = $this->getUser()->getId();
return $this->render('report/index.html.twig', ['reports' => $reportRepository->findAllByIdUser($user)]);
}
/**
* #Route("/new", name="report_new", methods="GET|POST")
*/
public function new(Request $request): Response
{
$report = new Report();
$form = $this->createForm(ReportType::class, $report);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($report);
$em->flush();
return $this->redirectToRoute('report_index');
}
return $this->render('report/new.html.twig', [
'report' => $report,
'form' => $form->createView(),
]);
}
/**
* #Route("/{id}", name="report_show", methods="GET")
*/
public function show(Report $report): Response
{
return $this->render('report/show.html.twig', ['report' => $report]);
}
/**
* #Route("/{id}/edit", name="report_edit", methods="GET|POST")
*/
public function edit(Request $request, Report $report): Response
{
$form = $this->createForm(ReportType::class, $report);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('report_edit', ['id' => $report->getId()]);
}
return $this->render('report/edit.html.twig', [
'report' => $report,
'form' => $form->createView(),
]);
}
/**
* #Route("/{id}", name="report_delete", methods="DELETE")
*/
public function delete(Request $request, Report $report): Response
{
if ($this->isCsrfTokenValid('delete'.$report->getId(), $request->request->get('_token'))) {
$em = $this->getDoctrine()->getManager();
$em->remove($report);
$em->flush();
}
return $this->redirectToRoute('report_index');
}
}
When I dump() my $form->getData()->getServices(), this return a collection. but i've this message : Neither the property "service" nor one of the methods "service()", "getservice()"/"isservice()"/"hasservice()" or "__call()" exist and have public access in class "Symfony\Component\Form\FormView".
on {{ form_widget(form.service, { 'attr': {'placeholder': "Service"} }) }}
Some people have a solution ?
I can provide more information if you need it
Thank you.

Symfony3, Edit Form for collection of images

I was following these guides: File upload tutorial and Collection form type guide. Everything was ok but "edit" action. Collection of images is shown correctly in "edit" action but when I try to submit the form (without adding any new images) i get an error.
Call to a member function guessExtension() on null
Which means that images in a collection got null values, instead of UploadedFile.
I've searched a lot and read many of sof questions about handling collection of images with doctrine but still no clue.
Questions like:
Symfony2, Edit Form, Upload Picture
howto handle edit forms with FileType inputs in symfony2
Multiple (oneToMany) Entities form generation with symfony2 and file upload
So the question is how can u handle edit images? Removal is needed too.
Controller edit action
public function editAction(Request $request, Stamp $stamp)
{
if (!$stamp) {
throw $this->createNotFoundException('No stamp found');
}
$form = $this->createForm(AdminStampForm::class, $stamp);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** #var Stamp $stamp */
$stamp = $form->getData();
foreach ($stamp->getImages() as $image) {
$fileName = md5(uniqid()) . '.' . $image->getName()->guessExtension();
/** #var $image StampImage */
$image->getName()->move(
$this->getParameter('stamps_images_directory'),
$fileName
);
$image->setName($fileName)->setFileName($fileName);
}
$em = $this->getDoctrine()->getManager();
$em->persist($stamp);
$em->flush();
$this->addFlash('success', 'Successfully edited a stamp!');
return $this->redirectToRoute('admin_stamps_list');
}
return [
'form' => $form->createView(),
];
}
Entity
<?php
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\JoinTable;
use Doctrine\ORM\Mapping\ManyToMany;
/**
* Class Stamp
* #package AppBundle\Entity
*
*
* #ORM\Entity(repositoryClass="AppBundle\Repository\StampRepository")
* #ORM\Table(name="stamp")
* #ORM\HasLifecycleCallbacks()
*/
class Stamp
{
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\Column(type="integer")
*/
private $id;
// * #ORM\OneToMany(targetEntity="AppBundle\Entity\StampImage", mappedBy="id", cascade={"persist", "remove"})
/**
* #ManyToMany(targetEntity="AppBundle\Entity\StampImage", cascade={"persist"})
* #JoinTable(name="stamps_images",
* joinColumns={#JoinColumn(name="stamp_id", referencedColumnName="id")},
* inverseJoinColumns={#JoinColumn(name="image_id", referencedColumnName="id", unique=true)}
* ) */
private $images;
/**
* #ORM\Column(type="datetime")
*/
private $createdAt;
/**
* #ORM\Column(type="datetime")
*/
private $updatedAt;
public function __construct()
{
$this->images = new ArrayCollection();
}
/**
* #return mixed
*/
public function getId()
{
return $this->id;
}
public function addImage(StampImage $image)
{
$this->images->add($image);
}
public function removeImage(StampImage $image)
{
$this->images->removeElement($image);
}
/**
* #return mixed
*/
public function getImages()
{
return $this->images;
}
/**
* #param mixed $images
*
* #return $this
*/
public function setImages($images)
{
$this->images = $images;
return $this;
}
/**
* #return mixed
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* #param mixed $createdAt
*
* #return Stamp
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* #return mixed
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* #param mixed $updatedAt
*
* #return Stamp
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* #ORM\PrePersist
* #ORM\PreUpdate
*/
public function updateTimestamps()
{
$this->setUpdatedAt(new \DateTime('now'));
if (null == $this->getCreatedAt()) {
$this->setCreatedAt(new \DateTime());
}
}
}
StampImage entity
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity as UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Class StampImage
* #package AppBundle\Entity
*
* #ORM\Entity()
* #ORM\Table(name="stamp_image")
* #UniqueEntity(fields={"name"}, message="This file name is already used.")
*/
class StampImage
{
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\Column(type="integer")
*/
private $id;
/**
* #return mixed
*/
public function getId()
{
return $this->id;
}
private $name;
/**
* #ORM\Column(type="string")
*/
private $fileName;
/**
* #return mixed
*/
public function getFileName()
{
return $this->fileName;
}
/**
* #param mixed $fileName
*
* #return StampImage
*/
public function setFileName($fileName)
{
$this->fileName = $fileName;
return $this;
}
/**
* #return mixed
*/
public function getName()
{
return $this->name;
}
/**
* #param mixed $name
*
* #return StampImage
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
}
Main entity form
<?php
namespace AppBundle\Form;
use AppBundle\Entity\Stamp;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class AdminStampForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('images', CollectionType::class, [
'entry_type' => AdminStampImageForm::class,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Stamp::class,
]);
}
public function getName()
{
return 'app_bundle_admin_stamp_form';
}
}
Image form type
<?php
namespace AppBundle\Form;
use AppBundle\Entity\StampImage;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class AdminStampImageForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', FileType::class, [
'image_name' => 'fileName',
'label' => false,
'attr' => [
],
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => StampImage::class,
'required' => false
]);
}
public function getBlockPrefix()
{
return 'app_bundle_admin_stamp_image_form';
}
}
FileType extension
- extends 'form_div_layout.html.twig'
- block file_widget
- spaceless
- if image_url is not null
%img{:src => "#{image_url}"}
%div{:style => "display: none;"}
#{block('form_widget')}
- else
#{block('form_widget')}
1) Multiupload (but not OneToMany).
2) For editing an uploaded image:
# AppBundle/Entity/Stamp
/**
* #ORM\Column(type="string", length=255)
*/
private $image;
# AppBundle/Form/StampForm
->add('imageFile', FileType::class, [ //the $image property of Stamp entity class will store the path to the file, and this imageFile field will get the uploaded file
'data_class' => null, //important!
])
#AppBundle/Controller/StampController
/**
* #Route("/{id}/edit", name="stamp_edit")
* #Method({"GET", "POST"})
*/
public function editAction(Request $request, Stamp $stamp) {
$editForm = $this->createForm(StampType::class, $stamp, [
'action'=>$this->generateUrl('stamp_edit',['id'=>$stamp->getId()]),
'method'=>'POST'
]);
$editForm->handleRequest($request);
if($editForm->isSubmitted() && $form->isValid()) {
$imageFile = $editForm->get('imageFile')->getData();
if (null != $imageFile) { //this means that for the current record that needs to be edited, the user has chosen a different image
//1. remove the old image
$oldImg = $this->getDoctrine()->getRepository('AppBundle:Stamp')->find($stamp);
$this->get('app.file_remove')->removeFile($oldImg->getImage());
//2. upload the new image
$img = $this->get('app.file_upload')->upload($imageFile);
//3. update the db, replacing the path to the old file with the path to the new uploaded file
$stamp->setImage($img);
$this->getDoctrine()->getManager()->flush();
//4. add a success flash, and anything else you need, and redirect to a route
} else { //if the user has chosen to edit a different field (but not the image one)
$this->getDoctrine()->getManager()->flush();
//add flash message, and redirect to a route
}
}
return $this->render(...);
}
#AppBundle/Services/FileRemove
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
class FileRemove {
private $targetDir;
public function __construct($targetDir) {
$this->targetDir = $targetDir;
}
public function removeFile($path) {
$fs = new Filesystem();
$file = $this->targetDir . '/' . $path;
try{
if($fs->exists($file)){
$fs->remove($file);
return true;
}
return false;
} catch(IOExceptionInterface $e){
//log error for $e->getPath();
}
}
}
#app/config/services.yml
app.file_remove:
class: AppBundle/Services/FileRemove
arguments: ['%stamp_dir%']
#app/config/config.yml
parameters:
stamp_dir: '%kernel.root_dir%/../web/uploads/stamps' //assuming this is how you've set up the upload directory
#AppBundle/Services/FileUpload
use Symfony\Component\HttpFoundation\File\UploadedFile;
class FileUpload{
private $targetDir;
public function __construct($targetDir) {
$this->targetDir = $targetDir;
}
public function upload(UploadedFile $file) {
$file_name = empty($file->getClientOriginalName()) ? md5(uniqid()).'.'.$file->guessExtension() : $file->getClientOriginalName();
$file->move($this->targetDir, $file_name);
return $file_name;
}
}
#app/config/services.yml
app.file_upload:
class: AppBundle\Services\FileUpload
arguments: ['%stamp_dir%']
Sorry for typos, and please let me know if this works for your case, as I adapted my case to yours.
Check you have uploaded image vie form or not before foreach.
if ($form->isSubmitted() && $form->isValid()) {
/** #var Stamp $stamp */
$stamp = $form->getData();
if(!empty($stamp->getImages()) && count($stamp->getImages()) > 0){ // Check uploaded image
foreach ($stamp->getImages() as $image) {
$fileName = md5(uniqid()) . '.' . $image->guessExtension();
/** #var $image StampImage */
$image->getName()->move(
$this->getParameter('stamps_images_directory'),
$fileName
);
$image->setName($fileName)->setFileName($fileName);
}
}
$em = $this->getDoctrine()->getManager();
$em->persist($stamp);
$em->flush();
$this->addFlash('success', 'Successfully edited a stamp!');
return $this->redirectToRoute('admin_stamps_list');
}
Update #1
Replace
$fileName = md5(uniqid()) . '.' . $image->getName()->guessExtension();
with
$fileName = md5(uniqid()) . '.' . $image->guessExtension();

Categories