Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8172005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:38:39+00:00 2026-06-06T21:38:39+00:00

I have a problem with saving the path in doctrine from a file upload.

  • 0

I have a problem with saving the path in doctrine from a file upload. I used the example from here: How to handle File Uploads with Doctrine

The upload works, but I don’t get the file path saved to the database and I think the callbacks don’t work. At least my uploads don’t have a file extension.

Here comes my controller:

   public function uploadAction()
{
$document = new Document();
//$form   = $this->createForm(new DocumentType(), $entity);


$form = $this->createFormBuilder($document)
    ->add('name')
    ->add('file')
    ->getForm()
;
if ($this->getRequest()->getMethod() === 'POST') {
    $form->bindRequest($this->getRequest());
    if ($form->isValid()) {

        $em = $this->getDoctrine()->getEntityManager();
        $em->persist($document);
        $em->flush();

        $this->redirect($this->generateUrl('document'));
    }
}

return  $this->render("ISClearanceBundle:Document:upload.html.twig",array('form' =>   $form->createView()));
}

Here is the Entity:

 <?php

   namespace IS\ClearanceBundle\Entity;

   use Doctrine\ORM\Mapping as ORM;
   use Symfony\Component\HttpFoundation\File\UploadedFile;
   use Symfony\Component\Validator\Constraints as Assert;

  /**
   * IS\ClearanceBundle\Entity\Document
   * @ORM\Entity
   * @ORM\HasLifecycleCallbacks
   */
  class Document
 {
 /**
  * @var string $name
  */
private $name;

/**
 *  @ORM\Column(type="string", length=255, nullable=true)
 */
public $path;

/**
 * @var integer $projectId
 */
private $projectId;

/**
 * @var integer $id
 */
private $id;

 /**
 * @Assert\File(maxSize="6000000")
 */
public $file;

/**
 * Set name
 *
 * @param string $name
 */
public function setName($name)
{
    $this->name = $name;
}

/**
 * Get name
 *
 * @return string
 */
public function getName()
{
    return $this->name;
}

/**
 * Set path
 *
 * @param integer $path
 */
public function setPath($path)
{
    $this->path = $path;
}

/**
 * Get path
 *
 * @return integer
 */
public function getPath()
{
    return $this->path;
}

/**
 * Set projectId
 *
 * @param integer $projectId
 */
public function setProjectId($projectId)
{
    $this->projectId = $projectId;
}

/**
 * Get projectId
 *
 * @return integer
 */
public function getProjectId()
{
    return $this->projectId;
}

/**
 * Get id
 *
 * @return integer
 */
public function getId()
{
    return $this->id;
}

 public function getAbsolutePath()
{
    return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->path;
}

public function getWebPath()
{
    return null === $this->path ? null : $this->getUploadDir().'/'.$this->path;
}

protected function getUploadRootDir()
{
    // the absolute directory path where uploaded documents should be saved
    return __DIR__.'/../../../../web/'.$this->getUploadDir();
}

protected function getUploadDir()
{
    // get rid of the __DIR__ so it doesn't screw when displaying uploaded doc/image in the view.
    return 'uploads/documents';
}

/**
 * @ORM\PrePersist()
 * @ORM\PreUpdate()
 */
public function preUpload()
{
    if (null !== $this->file) {
        // do whatever you want to generate a unique name
        $this->path = uniqid().'.'.$this->file->guessExtension();
    }
        $this->path = uniqid().'.'.$this->file->guessExtension();
}

/**
 * @ORM\PostPersist()
 * @ORM\PostUpdate()
 */
public function upload()
{
    if (null === $this->file) {
        return;
    }

    // if there is an error when moving the file, an exception will
    // be automatically thrown by move(). This will properly prevent
    // the entity from being persisted to the database on error
    $this->file->move($this->getUploadRootDir(), $this->path);
    $this->setPath($this->path);
    unset($this->file);
}

/**
 * @ORM\PostRemove()
 */
public function removeUpload()
{
    if ($file = $this->getAbsolutePath()) {
        unlink($file);
    }
 }
}

I really don’t know how to save the path to the database and how to debug.

Thanks for any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:38:40+00:00Added an answer on June 6, 2026 at 9:38 pm

    Look at upload() method. It have postPersist() which means the entity changes are NOT saved to DB, so changed properties won’t be stored in current save.

    You have to update path in method using prePersist annotation and move file in method using postPersist. Also don’t unset %this->file, just assign null value.

    Based on you class

    /**
     * @ORM\PrePersist()
     */
    public function preUpload()
    {
        if (null !== $this->file) {
            // do whatever you want to generate a unique name
            $this->path = uniqid().'.'.$this->file->guessExtension();
        }
    }
    
    /**
     * @ORM\PostPersist()
     */
    public function upload()
    {
         if (null === $this->file) {
             return;
         }
    
         $this->file->move($this->getUploadRootDir(), $this->path);
         $this->file = null;
    }
    

    And remember that using move should use absolute path on you server so e.g. /var/www/my_app/web/storage/uploads/file.jpg or C:\www\my_app\web\storage\uploads\file.jpg (don’t worry to much about slashed direction, if you use only “/” it should work fine)

    public static function getUploadRootDir(){
        return $_SERVER['DOCUMENT_ROOT'].'/'.$this->getUploadDir(); 
    }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have a problem saving 2 tables of my database at the same
I am using CakePHP framework with MySQL database and I have problem in saving
I have problem creating new instance of excel 2007 using VBA (from Access 2002).
I have a problem with modifying xml file when i first load and then
I am saving image into isolated storage each image have a different imageFileName. But
MVC3 VB.NET Application. I have multiple file upload boxes on a form in my
I have a requirement to download a PDF file and saving it on clicking
I have the following script (it looks long, but well commented). Problem is, i
In an iPhone app I currently have code for downloading a file from the
I have having a problem saving the sign up form using Devise. I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.