I have 2 entities: Audio and Destination
In Audio:
/**
* @ORM\OneToOne(targetEntity="HearWeGo\HearWeGoBundle\Entity\Destination", inversedBy="audio")
* @Assert\NotBlank(message="This field must be filled")
*
*/
private $destination;
I created a Form Type name EditAudioType used to edit an audio whose uploaded link is stored in database
<?php
namespace HearWeGo\HearWeGoBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use HearWeGo\HearWeGoBundle\Entity\Audio;
class AudioAudioType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$destination_repo=$options['dr'];
$builder
->add('name','text')
->add('audio','file')
->add('destination','entity',array(
'class'=>'HearWeGoHearWeGoBundle:Destination',
'choices'=>$destination_repo->findToReplaceAudio('id'),
'property'=>'name'
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array('data_class'=>"HearWeGo\\HearWeGoBundle\\Entity\\Audio"));
$resolver->setRequired(array('dr'));
}
public function getName()
{
return 'edit_audio';
}
}
I did as the similar answer in my old post: Symfony2 Entity Form Type gets data
But now I don't know what I will put in choices
attribute of form, because this custom repo function has parameter, not like the old answer. In DestinationRepository
:
public function findByAudioId($id)
{
return $this->getEntityManager()->createQuery('SELECT d FROM HearWeGoHearWeGoBundle:Destination d,HearWeGoHearWeGoBundle:Audio a WHERE d.id=IDENTITY (a.destination)')->getResult();
}
public function findToReplaceAudio($id)
{
$result=$this->findDestinationWithoutAudio();
$result[]=$this->findByAudioId($id);
return $result;
}