Question

I have two entities with OneToOne relation : Objectif and Image .

in sonata admin bundle , i would like to be able to remove an image from an objective ( image is defined in formField with a sonata_type_admin.

I'm able to tick the checkbox "delete" but when i click on update button, nothing change, the image entity is still here .here is my code :

/** objectiveAdmin form field **/
/**
 * @param FormMapper $formMapper
 */
protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->with("General")
            ->add('titre')
            ->add('description')
        ->with("Icone")
            ->add('image', 'sonata_type_admin', array(), array('required' => false, 'edit' => 'admin'))
    ;
}


/** objective entity class **/
    /**
     * @ORM\OneToOne(targetEntity="acme\Bundle\Entity\Image" , cascade={"persist"} , orphanRemoval=true )
     */
     private $image;

and finally a capture of my sonata admin objectif page :

enter image description here

thx so much for any answers !

Was it helpful?

Solution

i finally succeed to achieve my question by appending the preUpdate function of the parent admin class :

here is the code :

    public function preUpdate($objectif) {

    // i get the _delete variable of my sonata_type_admin Image widget
    $params             =  $this->getRequest()->request->get($this->getUniqid());
    $image              =  $params['image'];

    // if the checkbox is checked i set NULL to my objectif image
    if ( isset ( $image['_delete'] ) && !empty ( $image['_delete'] ) )
    {
        $objectif->setImage( null ) ;
    }else{
        $objectif->setImage($objectif->getImage());
    }

    $this->manageEmbeddedImageAdmins($objectif);
}

 // finally , i call my Image remove function to delete the media ...

OTHER TIPS

Setup parent entity:

/**
* @ORM\OneToOne(targetEntity="NameChildEntity", mappedBy="idForeignKey", cascade={"persist", "remove"}, orphanRemoval=true)
*
*/
private $image;

Setup child entity:

/** 
* @var \Objective
*
* @ORM\OneToOne(targetEntity="NameParentEntity", inversedBy="image")
* @ORM\JoinColumns({
*   @ORM\JoinColumn(name="id_foreign_key", referencedColumnName="id")
* })
*/
private $any_name;

Then run:

$ php app/console doctrine:generate:entities YourBundle:Objective

$ php app/console doctrine:generate:entities YourBundle:Image

That will create the necessary methods in your entities.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top