Symfony 3.加入和VichUploaderBundle的问题

时间:2017-11-04 17:46:32

标签: sql symfony vichuploaderbundle

我使用Vichuploaderbundle和EntryImg实体加载了几张图片。

这很成功!

我还有一个名为Entries的实体。 每个条目应与一个或多个图像链接

我想加入存储库中的两个实体。 错误是什么?

这是我的参赛作品实体

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
//use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
 * @ORM\Entity(repositoryClass="AppBundle\Repository\EntriesRepository")
 * @ORM\Table(name="entries")
 */
class Entries
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\EntryImg", mappedBy="entries")
     */
    private $id;

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

    /**
     * @ORM\Column(type="text")
     */
    private $description;

    /**
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User")
    * @ORM\JoinColumn()
    */
    private $user;



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

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getDescription()
    {
        return $this->description();
    }

    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    public function getUser()
    {
      return $this->user;
    }

    public function setUser(User $user)
    {
        $this->user = $user;
    }



}

我的图片实体

<?php

namespace AppBundle\Entity;


use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\File;
//use Symfony\Component\HttpFoundation\File\UploadedFile;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
* @ORM\Entity
* @ORM\Table(name="`entry_img`")
* @Vich\Uploadable
*/
class EntryImg
{


    /**
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    * @ORM\Column(type="integer")
    */
    protected $id;

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

    /**
     * @Assert\File(maxSize="2000k",mimeTypes={"image/png", "image/jpeg", "image/pjpeg"})
     * @Vich\UploadableField(mapping="entry_images", fileNameProperty="image")
     * @var File
     */
    private $imageFile;

    /**
      * @ORM\Column(type="datetime")
      * @var \DateTime
      */
     private $updatedAt;

     /**
     *  @ORM\ManyToOne(targetEntity="AppBundle\Entity\Entries")
     *  @ORM\JoinColumn(name="entries_id",referencedColumnName="id",nullable=true)
     */
     private $entry;

     public function setImageFile(File $image = null)
     {
         $this->imageFile = $image;

         // VERY IMPORTANT:
         // It is required that at least one field changes if you are using Doctrine,
         // otherwise the event listeners won't be called and the file is lost
         if ($image) {
             // if 'updatedAt' is not defined in your entity, use another property
             $this->updatedAt = new \DateTime('now');
         }
     }

     public function getImageFile()
     {
         return $this->imageFile;
     }

     public function setImage($image)
     {
         $this->image = $image;
     }

     public function getImage()
     {
         return $this->image;
     }

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


      public function getEntry(){

        return $this->entry();
      }

      public function setEntry(Entries $entry){

        $this->entry = $entry;
      }
}

和我的资料库

<?php
namespace AppBundle\Repository;

use AppBundle\Entity\Entries;
use AppBundle\Entity\EntryImg;
use Doctrine\ORM\EntityRepository;

class EntriesRepository extends EntityRepository
{
    public function findAllEntries($e_ids)
    {

        $query = $this->createQueryBuilder('e')
          ->leftJoin('e.id','entry_img');
        $i = 0;
        foreach($e_ids as $e_id){
            if($i==0){
              //var_dump($e_id);
              $query->where('e.id = :entries_'.$i)
              ->setParameter('entries_'.$i,$e_id['entry']);
            }else if($i>0){
              $query->orWhere('e.id = :entries_'.$i)
              ->setParameter('entries_'.$i,$e_id['entry']);
            }
            $i++;
          }
        $result = $query->getQuery()->execute();
        return $result;
    }

1 个答案:

答案 0 :(得分:1)

在您的班级参赛作品中,您有这一行:

@ORM\OneToMany(targetEntity="AppBundle\Entity\EntryImg", mappedBy="entries")

但在您的EntryImg类中,没有任何$ entries属性。

此外,如果1个条目可以有多个EntryImg,则需要使用arraycollection。

我认为您应该像这样重写您的Entries类:

use Doctrine\Common\Collections\ArrayCollection; //don't forget this line for the constructor
class Entries
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
       //look here I remove the relation and put it on $entriesImg property
     */
    private $id;

    /**
     * @ORM\Column(type="array")
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\EntryImg", mappedBy="entry") //look here I changed "entries" by "entry" which is the actual property in your EntryImg class. (you did not have any $entries property)
     */
    private $entriesImg;

     .. the rest of your properties


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

    public function addEntryImg(\AppBundle\Entity\EntryImg $entryImg)
    {
        $this->entriesImg[] = $entryImg;

        return $this;
    }

    public function removeEntryImg(\AppBundle\Entity\EntryImg $entryImg)
    {
        $this->entriesImg->removeElement($entryImg);
    } 
       // also don't forget to implement classic getters and setters for the $entriesImg property

不要忘记为你的实体实现一个toString方法,你需要它来进行Crud操作。

然后,稍后在您的存储库中,您忘记使用 - &gt; addSelect();像这样的方法

class EntriesRepository extends EntityRepository
{
    public function findAllEntries()
    {
        $query = $this->createQueryBuilder('e')
          ->leftJoin('e.entriesImg','i');
          ->addSelect('i')
          // whatever from your logic ....