简体   繁体   English

Symfony2实体选择问题

[英]Symfony2 entity choice issue

I have an entity form field in my Symfony2 project. 我的Symfony2项目中有一个entity表单字段。

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('productId', 'genemu_jquerychosen_entity', array(
        'class' => 'EMRSabaBundle:Product',
        'property' => 'name'
    ))
    ;
}

The Product entity has some objects like price, name, mode, & id I want to let user choose the product by name & see chosen produc's price, then submit product ID, not name. Product实体具有一些对象,例如价格,名称,模式和ID,我想让用户按名称选择产品并查看所选产品的价格,然后提交产品ID,而不是名称。

Is there any soloution? 有什么东西吗?

As per the documentation for this bundle: 根据此捆绑包的文档:

You can use all the core choice types from Symfony (choice, country, ...) and Doctrine (ORM and ODM), you just have to prefix the type name with genemu_jqueryselect2_* 您可以使用Symfony(选择,国家,...)和主义(ORM和ODM)中的所有核心选择类型,只需在类型名称前加上genemu_jqueryselect2_ *

This means you are using a "regular" entity field. 这意味着您正在使用“常规” entity字段。 You can do what you want by skipping the definition of property and relying on the __toString() method from EMRSabaBundle:Product : 您可以通过跳过property的定义并依靠EMRSabaBundle:Product__toString()方法来执行EMRSabaBundle:Product

[property] [属性]

This is the property that should be used for displaying the entities as text in the HTML element. 这是用于在HTML元素中将实体显示为文本的属性。 If left blank, the entity object will be cast into a string and so must have a __toString() method. 如果保留为空白,则实体对象将转换为字符串,因此必须具有__toString()方法。

Change your code to: 将您的代码更改为:

$builder->add('productId', 'genemu_jquerychosen_entity', array(
        'class' => 'EMRSabaBundle:Product'
    ))

And define the _toString() method in your EMRSabaBundle:Product object as follow: 并在EMRSabaBundle:Product对象中定义_toString()方法,如下所示:

public function __toString()
{
    return $this->name . ' (' . $this->price . ')';
}

http://symfony.com/doc/current/reference/forms/types/entity.html#property http://symfony.com/doc/current/reference/forms/types/entity.html#property

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM