我有1到n的关系,因此一个Brand
有很多Cars
。我想要做的是只创建一个Web表单,其中显示来自两个实体的所有字段。要做到这一点,我创建了一个表单类型,但我认为我做错了,因为我在尝试打印树枝中的表单字段时遇到错误。谁能告诉我我哪里做错了?
错误:
Method "brand" for object "Symfony\Component\Form\FormView" does not exist in CarBrandBundle:Default:both.html.twig at line 1
实体:
class BrandEntity
{
protected $name;
protected $origin;
//Followed by getters and setters
/**
* @ORM\ManyToOne(targetEntity="BrandEntity", inversedBy="car")
* @ORM\JoinColumn(name="brand_id", referencedColumnName="id", nullable=false)
* @var object $brand
*/
protected $brand;
}
class CarEntity
{
protected $model;
protected $price;
//Followed by getters and setters
/**
* @ORM\OneToMany(targetEntity = "CarEntity", mappedBy = "brand")
* @var object $car
*/
protected $car;
public function __construct()
{
$this->car = new ArrayCollection();
}
public function addCar(\Car\BrandBundle\Entity\CarEntity $car)
{
$this->car[] = $car;
return $this;
}
public function removeCar(\Car\BrandBundle\Entity\CarEntity $car)
{
$this->car->removeElement($car);
}
}
表单类型:
namespace Car\BrandBundle\Form\Type;
use Car\BrandBundle\Entity\BrandEntity;
use Car\BrandBundle\Entity\CarEntity;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Test\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class BothType extends AbstractType
{
public function builder(FormBuilderInterface $builder, array $options)
{
$builder
->setAction($options['action'])
->setMethod('POST')
->add('brand', new BrandEntity())
->add('car', new CarEntity())
->add('button', 'submit', array('label' => 'Add'))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
//'data_class' => 'Car\BrandBundle\Entity\CarEntity',
'cascade_validation' => true
));
}
public function getName()
{
return 'both';
}
}
控制器:
namespace Car\BrandBundle\Controller;
use Car\BrandBundle\Form\Type\BothType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class BothController extends Controller
{
public function indexAction()
{
$form = $this->getFrom();
return $this->render('CarBrandBundle:Default:both.html.twig',
array('page' => 'Both', 'form' => $form->createView()));
}
private function getFrom()
{
return $this->createForm(new BothType(), null,
array('action' => $this->generateUrl('bothCreate')));
}
}
嫩枝:
{{ form_row(form.brand.name) }}
{{ form_row(form.brand.origin) }}
{{ form_row(form.car.model) }}
{{ form_row(form.car.price) }}
答案 0 :(得分:1)
如果你想要一个"汽车形式"你需要选择一个品牌,然后另一个答案就可以了。
如果你想要的是一个"品牌形式"您可以在其中添加/编辑/删除多个汽车,然后您需要嵌入表格集合。
食谱答案在这里:http://symfony.com/doc/current/cookbook/form/form_collections.html
要呈现包含汽车表单集合的品牌表单(1-n关系):
表单类型如下所示:
品牌类型
// src/Acme/TaskBundle/Form/Type/BrandType.php
//...
class BrandType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('description');
$builder->add('cars', 'collection', array('type' => new CarType()));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\BrandBundle\Entity\Brand',
));
}
public function getName()
{
return 'brand';
}
}
车型:
namespace Acme\CarBundle\Form\Car;
//...
class TagType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\CarBundle\Entity\Car',
));
}
public function getName()
{
return 'car';
}
}
然后控制器和视图就像在食谱中一样。它最终非常强大和简单。
答案 1 :(得分:0)
要向表单添加实体,您必须使用实体字段类型:from the docs
$builder->add('users', 'entity', array(
'class' => 'AcmeHelloBundle:User',
'property' => 'username',
));
在这种情况下,所有用户对象都将从数据库加载并呈现为选择标记,集合或单选按钮或一系列复选框(这取决于多个和扩展值)。如果实体对象没有__toString()方法,则需要属性选项。
也请发布关系。