I have an Image entity:
class Image {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", nullable=true)
*/
protected $location;
/**
* @Assert\File(maxSize="6000000")
*/
protected $file;
public function __toString()
{
return $this->getLocation()
}
//............
}
And I want to allow users to delete select images and delete them. I made an ImageSelectType :
class ImageSelectType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(PROBLEM_IS_HERE, 'entity', array(
'class' => 'BloggerBlogBundle:Image',
)
)
;
}
//.......
}
However, I seem to be unable to understand exactly how Entity Type works. If I put ‘location’ or some property as first argument of $builder->add function, I get this for example:
object(Blogger\BlogBundle\Entity\Image)[316]
protected 'id' => null
protected 'location' =>
object(Blogger\BlogBundle\Entity\Image)[79]
protected 'id' => int 16
protected 'location' => string '8a307aadd466f1b92b149d3f79069f5a1abc9cd3.png' (length=44)
protected 'file' => null
protected 'file' => null
So It actually puts whole object into Location property of empty Image object. If I would have placed ‘id’ as first argument, I would get exactly the same result except object would be stored in $image->id. What am I supposed to enter as first argument of $builder-add to actually receive just the object itself?
Here is my action code if it’s relevant:
public function imageDeleteAction()
{
$image = new Image();
$request = $this->getRequest();
$em = $this->getDoctrine()->getEntityManager();
$form = $this->createForm(new ImageSelectType(), $image);
$form->bindRequest($request);
var_dump($image);
exit;
}
You need to create a type just for Image, which breaks out it’s properties in to separate fields, then use that AbstractType as the class for your other one.
You essentially want a collection of forms.
Update
I miss read your question a bit wrong the first time.
It looks like what you actually want to do is create something like an ImageGroup object which has an array of Images. Then that would be the target you send in to $this->createForm, and you would set that field in your PROBLEM_IS_HERE.
Then you would have to loop through the images and figure out which ones are missing to delete them. There are a few variations on this method, but this is probably the simplest.