我创建了一个基于实体字段的表单。此表单由允许用户选择实体的复选框组成(此处为' car')。 这工作正常但我需要自定义渲染以获得额外的信息。目前,显示的唯一信息是' id'属性。 在我的情况下,我想以颜色,品牌等形式显示额外的实体信息...... 有谁知道怎么办?
控制器:
public function chooserAction() {
//symfony.com/doc/current/reference/forms/types/entity.html
$cars = $this->getDoctrine()
->getRepository('CarBundle:Car')
->find(1);
$formBuilder = $this->createFormBuilder();
/*
foreach ($cars as $car) {
$formBuilder->add($car->getId() ,'checkbox')->getForm();
}
*/
$formBuilder->add('cars', 'entity', array(
'class' => 'CarBundle:Car',
'property' => 'id',
'expanded' => 'true',
'multiple' => 'true',
));
$formBuilder->add('save', 'submit');
$form = $formBuilder->getForm();
$request = $this->get('request');
$form->handleRequest($request);
if ($form->isValid()) {
echo "ok";
// return $this->redirect($this->generateUrl('car_show', array('id' => $car->getId())));
}
return $this->render('CarBundle:Car:chooser.html.twig', array('form' => $form->createView()));
}
实体:
<?php
namespace Foobar\CarBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Car
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Foobar\CarBundle\Entity\CarRepository")
*/
class Car
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="brand", type="string", length=255)
*/
private $brand;
/**
* @var string
*
* @ORM\Column(name="color", type="string", length=255)
*/
private $color;
/**
* @var integer
*
* @ORM\Column(name="power", type="integer")
*/
private $power;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Car
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set brand
*
* @param string $brand
* @return Car
*/
public function setBrand($brand)
{
$this->brand = $brand;
return $this;
}
/**
* Get brand
*
* @return string
*/
public function getBrand()
{
return $this->brand;
}
/**
* Set color
*
* @param string $color
* @return Car
*/
public function setColor($color)
{
$this->color = $color;
return $this;
}
/**
* Get color
*
* @return string
*/
public function getColor()
{
return $this->color;
}
/**
* Set power
*
* @param integer $power
* @return Car
*/
public function setPower($power)
{
$this->power = $power;
return $this;
}
/**
* Get power
*
* @return integer
*/
public function getPower()
{
return $this->power;
}
}
观点:
car chooser
{{ form(form) }}
答案 0 :(得分:1)
您可以在Car实体中创建一个toString()方法。
public function __toString()
{
return '' . $this->getName();
}
如果你想要更多的想法,你可以遵循http://symfony.com/doc/current/cookbook/form/form_customization.html