使用动态小部件构建表单
我有 3 个类:Category
、Parameter
和 Product
。
Category
与Parameters
具有一对多关系。Product
与Category
具有一对多关系。参数
是产品的属性(颜色、重量、尺寸、 品牌等)。
当我选择一个类别并创建新产品时,我想使用这些参数创建一个表单。 我该怎么做? symfony 表单框架可以吗? 我希望得到你的帮助。
我尝试这样做:
class ProductRepository extends EntityRepository
{
public function getParameters()
{
$em = $this->getEntityManager();
$parameters = $em->getRepository('ShopProductBundle:CatParameter')->findAll();
$data = array();
foreach ($parameters as $k => $value) {
$name = $value->getId();
$data[$name] = array("label" => $value->getName());
}
return $data;
}
}
表单类
class ProductType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('name', 'text', array('label' => 'Name'));
$data = $options['data'];
foreach($data as $k => $item){
$builder->add((string)$k, 'text', array('label' => $item['label']));
}
}
public function getName()
{
return 'shop_productbundle_categorytype';
}
public function getDefaultOptions(array $options){
return array('data_class' => 'Shop\ProductBundle\Entity\Product');
}
}
并在操作中创建表单:
$parameters = $em->getRepository('ShopProductBundle:Product')->getParameters();
$form = $this->createForm(new ProductType(), $parameters);
结束有异常:
预期参数类型为“Shop\ProductBundle\Entity\Product”、“array” 给定
I have 3 classes : Category
, Parameter
and Product
.
Category
has a one-to-many relationship withParameters
.Product
has a one-to-many relationship withCategory
.Parameters
are attributes for a product (color, weight, size,
brand, etc.).
When I select a category and create a new product I want to create a form with these parameters.
How can I do this? Is it possible with symfony form framework?
I hope for your help.
I trying do this something like:
class ProductRepository extends EntityRepository
{
public function getParameters()
{
$em = $this->getEntityManager();
$parameters = $em->getRepository('ShopProductBundle:CatParameter')->findAll();
$data = array();
foreach ($parameters as $k => $value) {
$name = $value->getId();
$data[$name] = array("label" => $value->getName());
}
return $data;
}
}
Form Class
class ProductType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('name', 'text', array('label' => 'Name'));
$data = $options['data'];
foreach($data as $k => $item){
$builder->add((string)$k, 'text', array('label' => $item['label']));
}
}
public function getName()
{
return 'shop_productbundle_categorytype';
}
public function getDefaultOptions(array $options){
return array('data_class' => 'Shop\ProductBundle\Entity\Product');
}
}
And create form in action:
$parameters = $em->getRepository('ShopProductBundle:Product')->getParameters();
$form = $this->createForm(new ProductType(), $parameters);
End have exception:
Expected argument of type "Shop\ProductBundle\Entity\Product", "array"
given
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(2)
涫野音2024-12-17 11:44:14
一旦您在表单中设置了数据类,您可以获得的唯一对象就是它的类型。
'data_class' => 'Shop\ProductBundle\Entity\Product'
我向产品实体添加多个参数的方式是表单项的集合:
如何添加项目集合
或者也许您只是想通过像这样的参数:
$parameters = $em->getRepository('ShopProductBundle:Product')->getParameters();
$form = $this->createForm(new ProductType(), null, $parameters);
或者如果您想要一个实体:
$form = $this->createForm(new ProductType(), new Product(), $parameters);
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我猜您会收到此异常,因为在您的控制器中
$parameters
不是产品。您应该构建一个新产品,并向其中添加参数。
另请查看这篇文章 。它处理与您类似的问题。
I guess you're getting this exception because in your controller
$parameters
is not a product.You should build a new product, and add the parameters to it.
Also please have a look to this article. It deals with a problem similar to yours.