如何在Symfony2中传递表单中的对象

问题描述:

我遇到问题。在开始的时候,这是我的行动,其创建表单:如何在Symfony2中传递表单中的对象

public function wyswietlDostepneTerminyAction($kategoria) { 
    $zlecenie = new Zlecenia(); 

    $form = $this->createForm(new ZleceniaAddType(), $zlecenie); 

    return array ('form' => $form->createView(), 'kategoria'=>$kategoria); 
} 

“Zlecenie”对象具有的“Kategorie”型(其从关系)“Kategoria”字段。

方法,坚持实体:

public function noweZlecenieAction(Request $request) { 
    $entity = new Zlecenia(); 
    $form = $this->createForm(new ZleceniaAddType(), $entity); 
    $form->bind($request); 

    if ($form->isValid()) { 
     $em = $this->getDoctrine()->getManager(); 
     $em->persist($entity); 
     $em->flush(); 

     return $this->redirect($this->generateUrl('pokaz-zlecenie', array('id' => $entity->getId()))); 
    } 

    return array(
     'entity' => $entity, 
     'form' => $form->createView(), 
    ); 
} 

,并形成类:

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add('opis') 
     ->add('klient', new KlientType()) 
     //->add('kategoria') 
    ; 
} 

通常情况下,我可以像添加字段:

->add('kategoria','entity', array('class'=>'Acme\MyBundle\Entity\Zlecenia') 

,并从列表中选择Kategoria。

但问题是:我不想从选择列表或复选框列表中选择Kategoria。我想使用预定义的$ kategoria对象。当然(如果必须存在)'分类'字段必须隐藏。我怎样才能做到这一点?

您可以创建一个数据转换器,将用户输入的数据转换为其他格式。 在你的情况下,你会将用户提供的Kategoria ID转换为Kategoria对象。这里最好的选择是定义你的表单中的“Kategoria”属性是隐藏的表单类型。 当表单呈现时,您将有一个隐藏的输入项,它将存储您的Kategoria对象的ID。当表单被submited时,变换器会将该ID反转为对应的Kategoria对象。由于您想要在创建Klient对象后在控制器中定义默认的Kategoria,您应该设置Kategoria。

如果你关注此源http://symfony.com/doc/master/cookbook/form/data_transformers.html,一切都会好起来的。任何问题,只是说

+0

这正是我一直在寻找!非常感谢你。 :) – user2279871

+0

很高兴帮助:) –

试试这个,调整到你的目录结构:Formtype:

public function buildForm(FormBuilderInterface $builder, array $options) { 
     if (isset($options['attr']['kategoria'])) { 
      $kategoria = $options['attr']['kategoria']; 
     } 
     else {$kategoria=null;} 
     $builder 
     ->add('opis') 
     ->add('klient', new KlientType()); 
     if ($kategoria) { 
      $transformer = new KategoriaTransformer($em); 
      $builder->add(
       $builder->create('kategoria' 
        ->add('kategoria', 'hidden', array() 
        ) 
       ) 
       ->addModelTransformer($transformer); 
      } else { 
       ->add('kategoria', 'entity' 
        , array('class'=>'Acme\MyBundle\Entity\Zlecenia') 
       } 
       ; 
      } 

和变压器:

namespace Acme\MyBundle\Entity\Zlecenia\Transformer; 

    use Doctrine\Common\Persistence\ObjectManager; 
    use Symfony\Component\Form\DataTransformerInterface; 
    use Symfony\Component\Form\Exception\TransformationFailedException; 

    class kategoriaTransformer implements DataTransformerInterface 
    { 
     /** 
     * @var ObjectManager 
     */ 
     private $em; 

     /** 
     * @param ObjectManager $em 
     */ 
     public function __construct(ObjectManager $em) 
     { 
      $this->em = $em; 
     } 

     /** 
     * Transforms an object (kategoria) to a string (id). 
     * 
     * @param Issue|null $kategoria 
     * @return string 
     */ 
     public function transform($kategoria) 
     { 
      if (null === $kategoria) {return "";} 
      if (is_object($kategoria) && 
       method_exists($kategoria, "toArray")){ 
       $kategoria=$kategoria->map(function ($ob){ 
        return $ob->getId();}); 
       return implode(",",$kategoria->toArray()); 
      } 
      return $kategoria->getId(); 
     } 

     /** 
     * Transforms a string (id) to an object (kategoria). 
     * 
     * @param string $id 
     * @return Issue|null 
     * @throws TransformationFailedException 
     *   if object (kategoria) is not found. 
     */ 
     public function reverseTransform($id) 
     { 
      if (!$id) { 
       if($this->multi) {return array();} 
       return null; 
      } 

      if (strpos($id,',') !== false) { 
       $id=explode(',',$id); 
      } 
      $qb=$this->em->getRepository(
      'Acme\MyBundle\Entity\Zlecenia\Repository\kategoria' 
      ) 
      ->createQueryBuilder('k'); 
      $qb->andWhere($qb->expr()->in('i.id', $id)); 
      if (is_array($id) || $this->multi){ 
       $kategoria=$qb->getQuery() 
       ->getResult(); 
      } else { 
       $kategoria=$qb->getQuery() 
       ->getSingleResult(); 
      } 
      if (null === $kategoria) { 
       throw new TransformationFailedException(sprintf(
        'A kategoria with id "%s" does not exist!', 
        $id 
        )); 
      } 

      return $kategoria; 
     } 
    }