New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How modify a image ? #89

Closed
Chrysweel opened this Issue Dec 20, 2012 · 10 comments

Comments

Projects
None yet
3 participants
@Chrysweel

Chrysweel commented Dec 20, 2012

Hello.
I need an example of a controller, which it upload a file.
i.e. I want modify a image, so I have to delete and create a new.
I have seen a option

 delete_on_update: true

But how does this work?

Thanks! : )

@ftassi

This comment has been minimized.

Show comment
Hide comment
@ftassi

ftassi Dec 20, 2012

Collaborator

It should delete the old image when you update a new one.

Is there something that is not clear in the doc ?

Collaborator

ftassi commented Dec 20, 2012

It should delete the old image when you update a new one.

Is there something that is not clear in the doc ?

@Chrysweel

This comment has been minimized.

Show comment
Hide comment
@Chrysweel

Chrysweel Dec 20, 2012

Exact, But How can I delete old image and to update it ?

Chrysweel commented Dec 20, 2012

Exact, But How can I delete old image and to update it ?

@ftassi

This comment has been minimized.

Show comment
Hide comment
@ftassi

ftassi Dec 20, 2012

Collaborator

Just upload a new one, the old should be deleted

Collaborator

ftassi commented Dec 20, 2012

Just upload a new one, the old should be deleted

@Chrysweel

This comment has been minimized.

Show comment
Hide comment
@Chrysweel

Chrysweel Dec 20, 2012

Yes @ftassi .
But I do this:

public function modifyImageAction($id)
    {
        $em = $this->get('doctrine')->getEntityManager();
        $imagen = $em->getRepository('sdfsBundle:Album')->findOneById($id);

        if ($imagen == null) {
            throw new NotFoundHttpException(The image doesnt exist '.$id);
        }
        $form = $this->get('form.factory')->create(new AlbumType());
        $form->setData($imagen);

        $request = $this->get('request');
        if ($request->getMethod() == 'POST')
        {
            $form->bindRequest($request);
            if ($form->isValid())
            {
                $em = $this->get('doctrine')->getEntityManager();
                $em->persist($imagen);
                $em->flush();
            }
        }
        return $this->render('sdfsBundle:Evento:modifyImage.html.twig',
                array('form' => $form->createView(),
                        'imagen'=>$imagen));
    } 

But the picture does not change :_(
What am I doing wrong?

Chrysweel commented Dec 20, 2012

Yes @ftassi .
But I do this:

public function modifyImageAction($id)
    {
        $em = $this->get('doctrine')->getEntityManager();
        $imagen = $em->getRepository('sdfsBundle:Album')->findOneById($id);

        if ($imagen == null) {
            throw new NotFoundHttpException(The image doesnt exist '.$id);
        }
        $form = $this->get('form.factory')->create(new AlbumType());
        $form->setData($imagen);

        $request = $this->get('request');
        if ($request->getMethod() == 'POST')
        {
            $form->bindRequest($request);
            if ($form->isValid())
            {
                $em = $this->get('doctrine')->getEntityManager();
                $em->persist($imagen);
                $em->flush();
            }
        }
        return $this->render('sdfsBundle:Evento:modifyImage.html.twig',
                array('form' => $form->createView(),
                        'imagen'=>$imagen));
    } 

But the picture does not change :_(
What am I doing wrong?

@ftassi

This comment has been minimized.

Show comment
Hide comment
@ftassi

ftassi Dec 20, 2012

Collaborator

Apparently nothing wrong in the controller, double check your configuration. The upload works fine? You're able to upload the image but not to updated it ?

The delete_on_update should just trigger the delete of the old one. Even if you've misconfigured it you should be able to upload a new image and bind it to the entity.

Collaborator

ftassi commented Dec 20, 2012

Apparently nothing wrong in the controller, double check your configuration. The upload works fine? You're able to upload the image but not to updated it ?

The delete_on_update should just trigger the delete of the old one. Even if you've misconfigured it you should be able to upload a new image and bind it to the entity.

@Chrysweel

This comment has been minimized.

Show comment
Hide comment
@Chrysweel

Chrysweel Dec 20, 2012

My entity is:

/**
 * @ORM\Table(name="Album")
 * @ORM\Entity
 * @Vich\Uploadable
 */
class Album {
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=255, name="image_name", nullable=true)
     */
    protected $imageName;
    /**
     * @Assert\File(
     *     maxSize="2000k",
     *     mimeTypes={"image/png", "image/jpeg", "image/pjpeg"}
     * )
     * @Vich\UploadableField(mapping="imagen_evento", fileNameProperty="imageName")
     *
     * @var File $image
     */
    public $image;
    /**
     * @ORM\Column(type="datetime", nullable=true)
     *
     * @Assert\DateTime
     */
    protected $updatedAt;

    /**
     * @ORM\ManyToOne(targetEntity="myproject\Entity\User", inversedBy="imagenes")
     * @ORM\JoinColumn(name="user", referencedColumnName="id")
     */
    protected $user;
    /**
     * @ORM\Column(type="datetime", nullable=true)
     *
     * @Assert\Date
     */
    protected $fecha_publicacion;
    /**
     * @ORM\Column(type="string", length=255, name="titulo", nullable=true)
     */
    public $titulo;


    public function __construct()
    {
        $this->fecha_publicacion = new \DateTime('now');
    }

My config.yml

knp_gaufrette:
    adapters:
        evento_adapter:
            local:
                directory: %kernel.root_dir%/../web/uploads/evento

    filesystems:
        imagen_evento_fs:
            adapter:    evento_adapter
    stream_wrapper: ~

vich_uploader:
    db_driver: orm
    gaufrette: true
    storage: vich_uploader.storage.gaufrette
    mappings:
        imagen_evento:
            uri_prefix: /workspace/project/web/uploads/imagenes
            upload_destination: imagen_evento_fs  
            namer: namer.media_file
            delete_on_remove: true
            delete_on_update: true

And my funciton upload is:

public function uploadAction($id)
    {
        $em = $this->get('doctrine')->getEntityManager();
        $e = $em->find('sdfsBundle:Evento', $id);
        $form = $this->get('form.factory')->create(new AlbumType());

        if ($this->getRequest()->isMethod('POST'))
        {
            $form->bind($this->getRequest());
            if ($form->isValid())
            {                   
                $foto = $form->getData();
                $foto->setEvento($e);
                $u = $this->get('security.context')->getToken()->getUser();
                $foto->setUser($u);
                $em = $this->get('doctrine')->getEntityManager();
                $em->persist($foto);
                $em->flush();

Something wrong with this?
Thanks for your help

Chrysweel commented Dec 20, 2012

My entity is:

/**
 * @ORM\Table(name="Album")
 * @ORM\Entity
 * @Vich\Uploadable
 */
class Album {
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=255, name="image_name", nullable=true)
     */
    protected $imageName;
    /**
     * @Assert\File(
     *     maxSize="2000k",
     *     mimeTypes={"image/png", "image/jpeg", "image/pjpeg"}
     * )
     * @Vich\UploadableField(mapping="imagen_evento", fileNameProperty="imageName")
     *
     * @var File $image
     */
    public $image;
    /**
     * @ORM\Column(type="datetime", nullable=true)
     *
     * @Assert\DateTime
     */
    protected $updatedAt;

    /**
     * @ORM\ManyToOne(targetEntity="myproject\Entity\User", inversedBy="imagenes")
     * @ORM\JoinColumn(name="user", referencedColumnName="id")
     */
    protected $user;
    /**
     * @ORM\Column(type="datetime", nullable=true)
     *
     * @Assert\Date
     */
    protected $fecha_publicacion;
    /**
     * @ORM\Column(type="string", length=255, name="titulo", nullable=true)
     */
    public $titulo;


    public function __construct()
    {
        $this->fecha_publicacion = new \DateTime('now');
    }

My config.yml

knp_gaufrette:
    adapters:
        evento_adapter:
            local:
                directory: %kernel.root_dir%/../web/uploads/evento

    filesystems:
        imagen_evento_fs:
            adapter:    evento_adapter
    stream_wrapper: ~

vich_uploader:
    db_driver: orm
    gaufrette: true
    storage: vich_uploader.storage.gaufrette
    mappings:
        imagen_evento:
            uri_prefix: /workspace/project/web/uploads/imagenes
            upload_destination: imagen_evento_fs  
            namer: namer.media_file
            delete_on_remove: true
            delete_on_update: true

And my funciton upload is:

public function uploadAction($id)
    {
        $em = $this->get('doctrine')->getEntityManager();
        $e = $em->find('sdfsBundle:Evento', $id);
        $form = $this->get('form.factory')->create(new AlbumType());

        if ($this->getRequest()->isMethod('POST'))
        {
            $form->bind($this->getRequest());
            if ($form->isValid())
            {                   
                $foto = $form->getData();
                $foto->setEvento($e);
                $u = $this->get('security.context')->getToken()->getUser();
                $foto->setUser($u);
                $em = $this->get('doctrine')->getEntityManager();
                $em->persist($foto);
                $em->flush();

Something wrong with this?
Thanks for your help

@Chrysweel

This comment has been minimized.

Show comment
Hide comment
@Chrysweel

Chrysweel Dec 20, 2012

Ahh and I have a service:

class VichServicio implements NamerInterface{

function name($obj, $field)
    {
            if ($obj->image){
                $titulo = $obj->titulo;
                    $extension = $file->guessExtension();           
                    return $titulo.'.'.$extension;

        }
}
}

Chrysweel commented Dec 20, 2012

Ahh and I have a service:

class VichServicio implements NamerInterface{

function name($obj, $field)
    {
            if ($obj->image){
                $titulo = $obj->titulo;
                    $extension = $file->guessExtension();           
                    return $titulo.'.'.$extension;

        }
}
}
@Chrysweel

This comment has been minimized.

Show comment
Hide comment
@Chrysweel

Chrysweel Dec 20, 2012

Anybody ?? Nothing ??

Do you how do to update an image?

Chrysweel commented Dec 20, 2012

Anybody ?? Nothing ??

Do you how do to update an image?

@jmather

This comment has been minimized.

Show comment
Hide comment
@jmather

jmather Dec 20, 2012

https://gist.github.com/4347146 is how I handle this -- it works in my testing anyway. :)

jmather commented Dec 20, 2012

https://gist.github.com/4347146 is how I handle this -- it works in my testing anyway. :)

@Chrysweel

This comment has been minimized.

Show comment
Hide comment
@Chrysweel

Chrysweel Dec 20, 2012

Ok thank you very much @jmather !!

It works. To other people with same problem, the entity has two fields, image and image_name, You have create a function:

  public function setImage($image)
    {
        $this->image = $image;

        if ($image instanceof UploadedFile) {
            $this->setUpdatedAt(new \DateTime());
        }
    }

Chrysweel commented Dec 20, 2012

Ok thank you very much @jmather !!

It works. To other people with same problem, the entity has two fields, image and image_name, You have create a function:

  public function setImage($image)
    {
        $this->image = $image;

        if ($image instanceof UploadedFile) {
            $this->setUpdatedAt(new \DateTime());
        }
    }

@Chrysweel Chrysweel closed this Dec 20, 2012

Koc added a commit to Koc/VichUploaderBundle that referenced this issue Sep 1, 2016

Koc added a commit to Koc/VichUploaderBundle that referenced this issue Sep 2, 2016

Koc added a commit to Koc/VichUploaderBundle that referenced this issue Sep 2, 2016

Koc added a commit to Koc/VichUploaderBundle that referenced this issue Sep 3, 2016

Koc added a commit to Koc/VichUploaderBundle that referenced this issue Sep 14, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment