1

The title may not be so clear so I'll explain in detail here (I must miss something obvious but I can't figure out what).

I'm using the Vich uploader bundle to store pictures in my project. I have two entity linked with an unidirectional one to one relation, the first is the owner and contain the annotation pointing to the second entity containing the file. This is the code part from the first entity :

...

     /**
     * @ORM\OneToOne(targetEntity="Cartong\MyBundle\Entity\Mysql\EntityContainingTheFile")
     * @ORM\JoinColumn(name="photo_id", referencedColumnName="id")
     */
    private $photo;

...

And the one containing the file :

 /**
 * @ORM\Entity
 * @ORM\HasLifecycleCallbacks()
 * @Vich\Uploadable
 */
class EntityContainingTheFile extends FileUpload
{
    /**
     * @var UploadedFile
     * @Vich\UploadableField(mapping="my_pictures", fileNameProperty="filename")
     */
    protected $file;

    /**
     * @return UploadedFile
     */
    public function getFile()
    {
        return parent::getFile();
    }

    /**
     * @param UploadedFile $file
     */
    public function setFile(File $file)
    {
        return parent::setFile($file);
    }
}

The FileUpload code is here too. It basically containing the file description (I'm using other entity that are extending this class) :

 /**
 * @ORM\MappedSuperclass
 * @Vich\Uploadable
 */
class FileUpload
{

    /**
     * @var integer : stock the unique id of the file
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string : stock the format of the file
     *
     * @ORM\Column(name="format", type="string", length=255)
     */
    private $format;

    /**
     * var string : stock the original name of the file
     *
     * @ORM\Column(name="alt", type="string", length=255)
     */
    private $alt;

    /**
     * @var integer : stock the size of the file (ko)
     *
     * @ORM\Column(name="size", type="integer")
     */
    private $size;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="updated_at", type="datetime", nullable=true)
     */
    private $updatedAt;

    /**
     * @var string $filename
     *
     * @ORM\Column(name="filename", type="string", length=255)
     */
    protected $filename;

    protected $file;

The file upload is working well, everything is stored at the right place in my project and the DB. The problem occur when I try to retrieve what I just store trough the first entity.

This is the kind of code I have in my controller :

$repo = $this->container->get('doctrine')->getRepository('CartongMSFBundle:MyFirstEntity');
        $test = $repo->find($theEntityWithAFile);

The object returned containing all the expected information except the photo, where all the fields are null.

So if I'm trying to get the specific file trough a findById in the "file" repo it's working but when I'm trying to get it trough my first entity it's not.

Any idea ? (maybe a mistake in the annotations ?)

iiirxs
  • 4,493
  • 2
  • 20
  • 35
Zoldir
  • 21
  • 1

1 Answers1

1

It seems like a typical doctrine hydration issue. In case of associations, doctrine by default doesn't load from database associated entities, until it is needed (e.g. you call $myFirstEntity->getPhoto()->getFormat()). This is called lazy loading.

If you want your associated entity to be loaded along with your first entity you should set doctrine fetch option to EAGER:

/**
 * @ORM\OneToOne(targetEntity="EntityContainingTheFile", fetch="EAGER")
 * @ORM\JoinColumn(name="photo_id", referencedColumnName="id")
 */
private $photo;
iiirxs
  • 4,493
  • 2
  • 20
  • 35