0

i'm having trouble to use the uploadable extension of stofdoctrinebundle

i've a File entity :

<?php

namespace my\TestBundle\Entity;

use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;

/**
 * File
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="my\TestBundle\Entity\FileRepository")
 * @Gedmo\Uploadable(path="uploads", filenameGenerator="SHA1", allowOverwrite=true, appendNumber=true)
 */
class File
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @ORM\Column(name="path", type="string")
     * @Gedmo\UploadableFilePath
     */
    private $path;

    /**
     * @ORM\Column(name="name", type="string")
     * @Gedmo\UploadableFileName
     */
    private $name;

    /**
     * @ORM\Column(name="mime_type", type="string")
     * @Gedmo\UploadableFileMimeType
     */
    private $mimeType;

    /**
     * @ORM\Column(name="size", type="decimal")
     * @Gedmo\UploadableFileSize
     */
    private $size;

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

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

    return $this;
    }

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

    /**
     * Set path
     *
     * @param string $path
     * @return File
     */
    public function setPath($path)
    {
    $this->path = $path;

    return $this;
    }

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

    /**
     * Set mimeType
     *
     * @param string $mimeType
     * @return File
     */
    public function setMimeType($mimeType)
    {
    $this->mimeType = $mimeType;

    return $this;
    }

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

    /**
     * Set size
     *
     * @param string $size
     * @return File
     */
    public function setSize($size)
    {
    $this->size = $size;

    return $this;
    }

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

in my controller, when i use a form directly on this entity :

$document = new File();
$form = $this->createFormBuilder($document)
        ->add('name')
        ->add('path','file',array(
                    'data_class' => null ))
        ->add('submit','submit')
        ->getForm()

if ($this->getRequest()->getMethod() === 'POST') {

    $form->handleRequest($this->getRequest());


    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();

        $em->persist($club);


        $uploadableManager = $this->get('stof_doctrine_extensions.uploadable.manager');

        $uploadableManager->markEntityToUpload($club, $club->getLogo()->getPath());

        $em->flush();


    }
}

my file is well uploaded and my entity correctly filled

But i want to use it in another entity :

class Company {
/**
 * @ORM\ManyToOne(targetEntity="my\TestBundle\Entity\File", cascade={"persist"})
 * @ORM\JoinColumn(name="logo", nullable=true)
 */
 private $logo;


/**
* Get logo
*
* @return \my\TestBundle\Entity\File 
*/
public function getLogo()
{
    return $this->logo;
}

/**
* Set comments
*
* @param string $comments
* @return Club
*/
public function setComments($comments)
{
    $this->comments = $comments;

    return $this;
}

And in my controller :

$company = new Company();
    $form = $this->createFormBuilder($company)
        ->add('name')
        ->add('logo', new \my\TestBundle\Form\FileType, array(
                    'data_class' => 'my\TestBundle\Entity\File' ))
        ->add('submit','submit')
        ->getForm()
    ;


    if ($this->getRequest()->getMethod() === 'POST') {

        $form->handleRequest($this->getRequest());


        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();

            $em->persist($club);


            $uploadableManager = $this->get('stof_doctrine_extensions.uploadable.manager');

            $uploadableManager->markEntityToUpload($company, $company->getLogo()->getPath());

            $em->flush();


        }
    }

My FileType :

/**
 *
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('path', 'file', array(
            'required' => false,
        ))
    ;
}

And when i submit, it tells me that all field of dile entity (mimetype, size, name) cannot be null. But normally they are filled with the extension (like in 1st case)

How can i manage this?

Thanks

Chuck Norris
  • 1,125
  • 1
  • 12
  • 28

2 Answers2

1

i think i solve my problem,

here what i've done :

    $company = new Company();
    $form = $this->createFormBuilder($company)
        ->add('name')
        ->add('logo', new \cM\ManagementBundle\Form\FileType, array(
                    'data_class' => 'cM\ManagementBundle\Entity\File' ))
        ->add('submit','submit')
        ->getForm()
    ;

    if ($this->getRequest()->getMethod() === 'POST') {
        $form->handleRequest($this->getRequest());
        if ($form->isValid()) {                
            $em = $this->getDoctrine()->getManager();    
            $em->persist($company);

            $uploadableManager = $this->get('stof_doctrine_extensions.uploadable.manager');
            $uploadableManager->markEntityToUpload($club->getLogo(), $club->getLogo()->getPath());

            $em->flush();
        }
    }
Chuck Norris
  • 1,125
  • 1
  • 12
  • 28
0

I don't think the name is handled. Check documentation, name is asked in every demo form.

Pierre Olivier Tran
  • 1,669
  • 4
  • 24
  • 40
  • Hi, thanks for the answer, but its not the problem, i've remove the 'name' field from the "non embedded" form (only with file entity, and so only the path field) and the form is submitted and entity correctly filled. – Chuck Norris Jan 26 '15 at 11:33