使用Symfony,我有一个来自实体的广播形式:
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 :(得分:0)
是的,有可能:
应用\实体\颜色:
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:
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
)
);
}
}
在控制器中:
/**
* @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()));
}
Twig模板:
<h1>Color product</h1>
{{ form_start(form) }}
{{ form_row(form.color) }}
{{ form_end(form) }}
结果: