1

I load several pictures with the Vichuploaderbundle and the EntryImg Entity.

That is successful!

I have also an Entity named Entries. Each Entry should be linked with one or more Images

I want to join both Entities in the Repository. What is the error?

Here is my Entries Entity

<?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;
    }



}

My Image Entity

<?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;
      }
}

and my repository

<?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;
    }
Adares
  • 9
  • 3
  • What error message do you receive? Did you run bin/console doctrine:schema:validate to check your entities? – colonelclick Nov 04 '17 at 18:13
  • In your EntryImg class you don't have any $entries property as you mentionned in your relation annotation -> @ORM\OneToMany(targetEntity="AppBundle\Entity\EntryImg", mappedBy="entries") – Mz1907 Nov 04 '17 at 18:42

1 Answers1

1

In your class Entries, you have this line:

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

But in your EntryImg class, there isn't any $entries property.

Also, if 1 Entries can have many EntryImg, you need to use arraycollection.

I think you should rewrite your Entries class like this:

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

Don't forget to implement a toString method for both your entities, you will need it for Crud operation.

Then, later in your repository, you forgot to use the ->addSelect(); method like this

class EntriesRepository extends EntityRepository
{
    public function findAllEntries()
    {
        $query = $this->createQueryBuilder('e')
          ->leftJoin('e.entriesImg','i');
          ->addSelect('i')
          // whatever from your logic ....
Pang
  • 9,564
  • 146
  • 81
  • 122
Mz1907
  • 625
  • 4
  • 10
  • i have made your changes. Now, when i am trying to save an entry an error is thrown. Could not resolve type of column "entriesImg" of class "AppBundle\Entity\Entries – Adares Nov 17 '17 at 11:07
  • and when i want to show my entries : Class AppBundle\Entity\Entries has no association named entriesImg – Adares Nov 17 '17 at 11:10