【问题标题】:Symfony 3. Problems with join and the VichUploaderBundleSymfony 3. join 和 VichUploaderBundle 的问题
【发布时间】:2017-11-04 17:46:32
【问题描述】:

我使用 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;
    }

【问题讨论】:

  • 您收到什么错误信息?您是否运行 bin/console dictionary:schema:validate 来检查您的实体?
  • 在您的 EntryImg 类中,您没有在关系注释中提到的任何 $entries 属性 -> @ORM\OneToMany(targetEntity="AppBundle\Entity\EntryImg", mappedBy="entries" )

标签: sql symfony vichuploaderbundle


【解决方案1】:

在您的班级条目中,您有这一行:

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

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

另外,如果1个Entries可以有多个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 操作。

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

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

【讨论】:

  • 我已对您进行了更改。现在,当我尝试保存条目时,会引发错误。无法解析类“AppBundle\Entity\Entries 的列“entriesImg”的类型
  • 当我想显示我的条目时:类 AppBundle\Entity\Entries 没有名为 entriesImg 的关联
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-24
  • 1970-01-01
  • 2018-05-19
  • 2018-10-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多