Problems with multiple upload images easyadmin

Hello I have several problems with the easyadmin multiple image uploads. Please help. Strangely I was having problems mostly with cakes and cakephotos. In easyadmin I add several images in Cakes, they get converted to array and stored in the db. After they get outputed when showed. Atleast that what I was planning to do…

Here is the Cakes.php entity

<?php

namespace AppEntity;

use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use VichUploaderBundleEntityFile as EmbeddedFile;
use VichUploaderBundleMappingAnnotation as Vich;

/**
 * @ORMEntity(repositoryClass="AppRepositoryCakesRepository")
 * @VichUploadable
 */
class Cakes
{
    /**
     * @ORMId()
     * @ORMGeneratedValue()
     * @ORMColumn(type="integer")
     */
    private $id;

    /**
     * @ORMColumn(type="string", length=100)
     */
    private $CakeName;

    /**
     * @ORMColumn(type="integer")
     */
    private $CakePrice;

    /**
     * @ORMColumn(type="string", length=255)
     */
    private $Description;

    /**
     * @ORMColumn(type="json")
     */
    private $CakeCategory = [];

    /**
     * @ORMManyToMany(targetEntity="Location", mappedBy="Cakes", cascade={"persist"})
     */
    private $AvailableLocation = [];

    /**
     * @ORMOneToMany(targetEntity="CakePhotos", mappedBy="Cake", cascade={"persist"})
     */
    private $CakePhotos;


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

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

    public function getCakeName(): ?string
    {
        return $this->CakeName;
    }

    public function setCakeName(string $CakeName): self
    {
        $this->CakeName = $CakeName;

        return $this;
    }

    public function getCakePrice(): ?int
    {
        return $this->CakePrice;
    }

    public function setCakePrice(int $CakePrice): self
    {
        $this->CakePrice = $CakePrice;

        return $this;
    }

    public function getDescription(): ?string
    {
        return $this->Description;
    }

    public function setDescription(string $Description): self
    {
        $this->Description = $Description;

        return $this;
    }

    public function getCakeCategory(): ?array
    {
        return $this->CakeCategory;
    }

    public function setCakeCategory(array $CakeCategory): self
    {
        $this->CakeCategory = $CakeCategory;

        return $this;
    }

    /**
     * @return Collection|Location[]
     */
    public function getAvailableLocation(): Collection
    {
        return $this->AvailableLocation;
    }
    public function addAvailableLocation(Location $location): self
    {
        if (!$this->AvailableLocation->contains($location)) {
            $this->AvailableLocation[] = $location;
            $location->addCake($this);
        }
        return $this;
    }
    public function removeAvailableLocation(Location $location): self
    {
        if ($this->AvailableLocation->contains($location)) {
            $this->AvailableLocation->removeElement($location);
            $location->removeCake($this);
        }
        return $this;
    }


    /**
     * @return Collection|CakePhoto[]
     */
    public function getCakePhotos(): Collection
    {
        return $this->CakePhotos;
    }
    public function addCakePhoto(CakePhotos $CakePhotos): self
    {
        if (!$this->CakePhotos->contains($CakePhotos)) {
            $this->CakePhoto[] = $CakePhotos;
            $CakePhotos->setCake($this);
        }
        return $this;
    }
    public function removeCakePhoto(CakePhotos $CakePhotos): self
    {
        if ($this->CakePhotos->contains($CakePhotos)) {
            $this->CakePhotos->removeElement($CakePhotos);
        }
        return $this;
    }

    public function getImageURL(): ?string
    {
        return $this->ImageURL;
    }

    public function setImageURL(string $ImageURL): self
    {
        $this->ImageURL = $ImageURL;

        return $this;
    }
}

Here is the CakePhotos.php entity

    <?php

namespace AppEntity;

use DoctrineORMMapping as ORM;
use SymfonyComponentHttpFoundationFileFile;
use VichUploaderBundleMappingAnnotation as Vich;

/**
 * @ORMEntity(repositoryClass="AppRepositoryCakePhotosRepository")
 * @VichUploadable
 */
class CakePhotos
{
    /**
     * @ORMId()
     * @ORMGeneratedValue()
     * @ORMColumn(type="integer")
     */
    private $id;

    /**
     * @ORMColumn(type="string", length=255, nullable=true)
     */
    private $Image;

    /**
     * @VichUploadableField(mapping="cakephotos", fileNameProperty="Image")
     */
    private $ImageFile;

    /**
     * @ORMManyToOne(targetEntity="Cakes", inversedBy="CakePhotos")
     */
    private $Cake;


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

    public function getImage(): ?string
    {
        return $this->Image;
    }

    public function setImage(?string $Image)
    {
        $this->Image = $Image;

        return $this;
    }
    public function getCake(): ?Cakes
    {
        return $this->Cake;
    }
    public function setCake(?Cake $cake): self
    {
        $this->Cake = $cake;
        return $this;
    }

    public function __toString()
    {
        return $this->Image;
    }

    /**
     * @return mixed
     */
    public function getImageFile()
    {
        return $this->ImageFile;
    }
    /**
     * @param mixed $imageFile
     * @throws Exception
     */
    public function setImageFile(?File $ImageFile): void
    {
        $this->ImageFile = $ImageFile;
    }
}

Here is my Form for converting

    <?php
namespace AppForm;
use AppEntityCakePhotos;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
use VichUploaderBundleFormTypeVichFileType;
class ImageType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('ImageFile', VichFileType::class)
        ;
    }
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => CakePhotos::class,
        ]);
    }
}

All around the code I have different problems such as:

Argument 1 passed to AppEntityCakePhotos::setCake() must be an instance of AppEntityCake or null, instance of AppEntityCakes given, called in /home/kumaskano1/Desktop/Learning/Symfony/Shirin/src/Entity/Cakes.php

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *