2017-11-11 172 views
0

隨着Symfony的,我有從實體無線電形式:Symfony的:獲取實體radio_widget

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add('color', EntityType::class, [ 
      'class' => Color::class, 
      'expanded' => true, 
      'label_attr' => [ 
       'class' => 'radio-inline' 
      ], 
      'label' => 'label.color', 
      .... 

但是,當我轉儲()在radio_widget,我沒有實體:

array:28 [▼ 
    "value" => "1" 
    "attr" => [] 
    "form" => FormView {#975 ▶} 
    "id" => "news_category_versions_0_color_1" 
    "name" => "1" 
    "full_name" => "news_category[versions][0][color]" 
    "disabled" => false 
    "label" => "Blue" 
    "label_format" => null 
    "multipart" => false 
    "block_prefixes" => array:4 [▶] 
    "unique_block_prefix" => "_news_category_versions_entry_color_entry" 
    "translation_domain" => false 
    "cache_key" => "_news_category_versions_entry_color_entry_radio" 
    "errors" => FormErrorIterator {#1186 ▶} 
    "valid" => true 
    "data" => false 
    "required" => true 
    "size" => null 
    "label_attr" => [] 
    "compound" => false 
    "method" => "POST" 
    "action" => "" 
    "submitted" => false 
    "help_translation_domain" => null 
    "checked" => false 
    "parent_label_class" => "radio-inline" 
    "app" => AppVariable {#1216 ▶} 
] 

我可以在radio_widget中獲得實體嗎?每個收音機都有一個特定的顏色(藍色,紅色,綠色等)。

回答

0

是的,它是可能的:

應用程序\實體\顏色:

namespace AppBundle\Form; 


use AppBundle\Entity\Color; 
use AppBundle\Entity\ColorProduct; 
use Doctrine\ORM\EntityRepository; 
use Symfony\Bridge\Doctrine\Form\Type\EntityType; 
use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolver; 

class ColorProductType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('color', EntityType::class, array(
      'class'   => Color::class, 
      'choice_label' => 'name', 
      'query_builder' => function (EntityRepository $er) { 
       return $er->createQueryBuilder('c'); 
      }, 
      'expanded' => true, 
      'required' => true, 
     )); 
    } 

    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => ColorProduct::class 
      ) 
     ); 
    } 
} 

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Class Color 
* 
* @package AppBundle\Entity 
* 
* @ORM\Table() 
* @ORM\HasLifecycleCallbacks() 
* @ORM\Entity(repositoryClass="AppBundle\Entity\ColorRepository") 
*/ 
class Color 
{ 
    /** 
    * @var int $id 
    * @ORM\Id() 
    * @ORM\Column() 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    /** 
    * @var string $name 
    * @ORM\Column(type="string", length=255) 
    */ 
    protected $name; 

    /** 
    * @ORM\OneToMany(targetEntity="ColorProduct", mappedBy="color", cascade={"all"}) 
    */ 
    protected $product; 

    /** 
    * Constructor 
    */ 
    public function __construct() 
    { 
     $this->product = new \Doctrine\Common\Collections\ArrayCollection(); 
    } 

    /** 
    * Get id 
    * 
    * @return string 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set name 
    * 
    * @param string $name 
    * 
    * @return Color 
    */ 
    public function setName($name) 
    { 
     $this->name = $name; 

     return $this; 
    } 

    /** 
    * Get name 
    * 
    * @return string 
    */ 
    public function getName() 
    { 
     return $this->name; 
    } 

    /** 
    * Add product 
    * 
    * @param \AppBundle\Entity\ColorProduct $product 
    * 
    * @return Color 
    */ 
    public function addProduct(\AppBundle\Entity\ColorProduct $product) 
    { 
     $this->product[] = $product; 

     return $this; 
    } 

    /** 
    * Remove product 
    * 
    * @param \AppBundle\Entity\ColorProduct $product 
    */ 
    public function removeProduct(\AppBundle\Entity\ColorProduct $product) 
    { 
     $this->product->removeElement($product); 
    } 

    /** 
    * Get product 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getProduct() 
    { 
     return $this->product; 
    } 
} 

在ColorProductEntity

namespace AppBundle\Entity; 

use Symfony\Component\Validator\Constraints as Assert; 
use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Table() 
* @ORM\HasLifecycleCallbacks() 
* @ORM\Entity(repositoryClass="AppBundle\Entity\ColorRepository") 
* 
*/ 
class ColorProduct 
{ 
    /** 
    * @var int $id 
    * @ORM\Id() 
    * @ORM\Column() 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    /** 
    * @ORM\ManyToOne(targetEntity="Color", inversedBy="product") 
    * @ORM\JoinColumn(name="color_id", referencedColumnName="id") 
    */ 
    protected $color; 




    /** 
    * Get id 
    * 
    * @return string 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set color 
    * 
    * @param \AppBundle\Entity\Color $color 
    * 
    * @return ColorProduct 
    */ 
    public function setColor(\AppBundle\Entity\Color $color = null) 
    { 
     $this->color = $color; 

     return $this; 
    } 

    /** 
    * Get color 
    * 
    * @return \AppBundle\Entity\Color 
    */ 
    public function getColor() 
    { 
     return $this->color; 
    } 
} 

爲colorProduct的formType與顏色實體一個關係

在控制器:

/** 
    * @Route("/product/color", name="product_color") 
    * @param \Symfony\Component\HttpFoundation\Request $request 
    * 
    * @return \Symfony\Component\HttpFoundation\Response 
    */ 
    public function addColorProduct(Request $request) { 
     $colorType = new ColorProduct(); 
     $form = $this->createForm(ColorProductType::class, $colorType); 

     $form->handleRequest($request); 

     if ($form->isSubmitted()) { 
      //......... 
     } 

     return $this->render('AppBundle:product:color.html.twig', array('form' => $form->createView())); 
    } 

嫩枝模板:

<h1>Color product</h1> 

{{ form_start(form) }} 
    {{ form_row(form.color) }} 
{{ form_end(form) }} 

其結果是:

enter image description here