Quantcast
Channel: Active questions tagged symfony4 - Stack Overflow
Viewing all articles
Browse latest Browse all 3924

Call to a member function guessExtension() on string with relation one to many

$
0
0

I'm trying to upload several pictures in Symfony 4.4 but I got this error:

Call to a member function guessExtension() on string

I have ManyToOne relation between Event and Picture. Each Event can be associated with many Pictures but, each picture can be associated with only one Event.

My entity Event :

/**
 * @ORM\Entity(repositoryClass="App\Repository\EventRepository")
 */
class Event
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $title;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Picture", mappedBy="event")
     */
    private $pictures;

    /**
     * getter and setter for $this->title
     */

    public function __construct()
    {
        $this->pictures = new ArrayCollection();
    }

    /**
     * @return Collection|Picture[]
     */
    public function getPictures()
    {
        return $this->pictures;
    }

    public function addPicture(Picture $picture)
    {
        if (!$this->pictures->contains($picture)) {
            $this->pictures[] = $picture;
            $picture->setEvent($this);
        }

        return $this;
    }

    public function removePicture(Picture $picture)
    {
        if ($this->pictures->contains($picture)) {
            $this->pictures->removeElement($picture);
            // set the owning side to null (unless already changed)
            if ($picture->getEvent() === $this) {
                $picture->setEvent(null);
            }
        }

        return $this;
    }
}

My entity Picture :

/**
 * @ORM\Entity(repositoryClass="App\Repository\PictureRepository")
 */
class Picture
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $name;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Event", inversedBy="pictures")
     * @ORM\JoinColumn(nullable=false)
     */
    private $event;

    public function getId(): ?int
    {
        return $this->id;
    }

    /**
     * getter and setter for $this->name
     */

    public function getEvent()
    {
        return $this->event;
    }

    public function setEvent(?Event $event)
    {
        $this->event = $event;

        return $this;
    }
}

EventType Form :

    $builder
        ->add('title', TextType::class)
        ->add('pictures', CollectionType::class, [
            'entry_type' => PictureType::class,
            'allow_add' => true,
            'allow_delete' => true,
            'prototype' => true,
            'by_reference' => false,
            'label' => false
        ])
    ;

PictureType Form

    $builder
        ->add('name', FileType::class, [
            'data_class' => null,
            'label' => ''
        ])
    ;

My controller

/**
 * @Route("/new", name="admin-spectacle-new")
 */
public function new(Request $request)
{
    $event = new Event();
    $form = $this->createForm(EventType::class, $event);
    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
        $images = $form->get('pictures')->getData();
        foreach ($images as $image) {
                $fileName = md5(uniqid()).'.'.$image->getName()->guessExtension();
                $image->move($this->getParameter('image_spectacle'), $fileName);
                $image->setName($fileName);
        }
        //...
    }
    return $this->render(...);
}

Any ideas why I am getting this error?


Viewing all articles
Browse latest Browse all 3924


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>