Проблемы с несколькими загрузками изображений easyadmin - PullRequest
0 голосов
/ 12 января 2020

Здравствуйте. У меня несколько проблем с загрузкой изображений easyadmin. Пожалуйста помоги. Странно, у меня были проблемы в основном с пирожными и пирожными. В easyadmin я добавляю несколько изображений в Cakes, они преобразуются в массив и сохраняются в БД. После того, как они получили, когда показали. По крайней мере, то, что я планировал сделать ...

Вот торты. php сущность

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Vich\UploaderBundle\Entity\File as EmbeddedFile;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

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

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

    /**
     * @ORM\Column(type="integer")
     */
    private $CakePrice;

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

    /**
     * @ORM\Column(type="json")
     */
    private $CakeCategory = [];

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

    /**
     * @ORM\OneToMany(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;
    }
}

Вот CakePhotos. php сущность

    <?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

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

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

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

    /**
     * @ORM\ManyToOne(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;
    }
}

Вот моя Форма для конвертации

    <?php
namespace App\Form;
use App\Entity\CakePhotos;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Vich\UploaderBundle\Form\Type\VichFileType;
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,
        ]);
    }
}

По всему коду у меня разные проблемы, такие как:

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