How to get web directory path from inside Entity?

17,652

Solution 1

You shouldn't use entity class as a form model here. It's simply not suitable for that job. If the entity has the path property, the only valid values it can stores are: null (in case lack of the file) and string representing the path to the file.

  1. Create a separate class, that's gonna be a model for your form:

    class MyFormModel {
         /** @Assert\File */
         private $file;
    
         /** @Assert\Valid */
         private $entity;
    
         // constructor, getters, setters, other methods
    }
    
  2. In your form handler (separate object configured through DIC; recommended) or the controller:

    ...
    if ($form->isValid()) {
        /** @var \Symfony\Component\HttpFoundation\File\UploadedFile */
        $file   = $form->getData()->getFile();
    
        /** @var \Your\Entity\Class */
        $entity = $form->getData()->getEntity();
    
        // move the file
        // $path = '/path/to/the/moved/file';
    
        $entity->setPath($path);
    
        $someEntityManager->persist($entity);
    
        return ...;
    }
    ...
    

Inside form handler/controller you can access any dependencies/properties from DIC (including path to the upload directory).


The tutorial you've linked works, but it's an example of bad design. The entities should not be aware of file upload.

Solution 2

To access the root directory from outside the controller you can simply inject '%kernel.root_dir%' as an argument in your services configuration.

service_name:
    class: Namespace\Bundle\etc
    arguments: ['%kernel.root_dir%']

Then you can get the web root in the class constructor:

public function __construct($rootDir)
{
    $this->webRoot = realpath($rootDir . '/../web');
}
Share:
17,652
Mikhail
Author by

Mikhail

CTO at Umbrella-web company Umbrella Web Studio offers you the creation of sharp, up-to-date Web applications with extensible functionality and scalability for high traffic loads. The company offers a wide range of experience in the creation of successful commercial projects.

Updated on August 22, 2022

Comments

  • Mikhail
    Mikhail over 1 year

    I researched the How to Handle File Uploads with Doctrine and I don't want to hard-code the __DIR__.'/../../../../web/'.$this->getUploadDir(); path because maybe in future I will change the web/ directory. How to do it more flexible? I found this but it doesn't answer the question how to do it more flexible from inside the Entity

  • Mikhail
    Mikhail over 11 years
    I understood. Thanks. But how to use a lifecycle callbacks to manage a deletion of the file with your code?
  • Jovan Perovic
    Jovan Perovic over 11 years
    Based on previous experience I suggest you store absolute path (or at least relative to webroot path) as well..
  • Crozin
    Crozin over 11 years
    Create a listener, that subscribes preRemove (or preDelete) event (see how to) and unlink the file there.
  • Mikhail
    Mikhail over 11 years
    How to obtain it inside an Entity?
  • Benjamin Lazarecki
    Benjamin Lazarecki over 11 years
    It's not the responsibility of your entity to know where the document is store. Yo can move this responsibility to your controller. Or you can add a path attribute to your entity
  • Mikhail
    Mikhail over 10 years
    Hm, I see that you used the __DIR__ . "/../../../../web/" too, but my question is how to avoid it.
  • Tom Phillips
    Tom Phillips over 10 years
    I'm not sure there is one. It seemed like the best i could do was define it in one place.
  • Mikhail
    Mikhail over 10 years
    got it. Thanks for your solution Tom