Symfony2 Entity Form Type gets data from another Entity

橙三吉。 提交于 2019-12-25 04:50:14

问题


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;
        }

回答1:


I'll do something like :

<?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;
use HearWeGo\HearWeGoBundle\Entity\Repository\AudioRepository;

class AudioAudioType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name','text')
            ->add('audio','file')
            ->add('destination','entity',array(
                'class'=>'HearWeGoHearWeGoBundle:Destination',
                'query_builder'=>function (AudioRepository $repository) {
                    return $repository->findToReplaceAudioQueryBuilder('id');
                },
                'property' => 'name'
            ));
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array('data_class'=>"HearWeGo\\HearWeGoBundle\\Entity\\Audio"));
    }

    public function getName()
    {
        return 'edit_audio';
    }
}

Please have a look to the official documentation.



来源:https://stackoverflow.com/questions/31695773/symfony2-entity-form-type-gets-data-from-another-entity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!