【发布时间】:2015-02-20 18:38:27
【问题描述】:
由于实体字段类型,我有一个用户可以选择的实体(产品)。 选定的产品成为一个新的实体,userIngredient 通过 oneToMany 关系与产品链接(一个产品对应多个 userIngredients)。
现在,当创建一个食谱时,用户应该只选择他添加到列表中的产品,所以我使用查询构建器来过滤产品并检查它们是否有用户对应的 userIngredient。
这很好用,但我也希望用户根据其 userIngredient 名称而不是其产品名称来选择产品。 所以我需要从产品方=多方查询 oneToMany 关系。这是问题:在产品实体中,我无法调用实体经理。
我需要的不是父实体而是实际产品上的预设数据事件。怎么做?
$builder
->add('product', 'genemu_jqueryselect2_entity', array(
'label'=>'Ingrédient',
'multiple' => false,
'required' => false,
'class' => 'AppBundle:MarketPlace\Product',
'property' => 'getUserIngredientName',
'query_builder'=>$this->queryBuilder,
'attr'=>array(
'data-toggle'=>"tooltip",
'data-placement'=>"top",
'title'=>"Choisissez votre ingrédient. Seuls les ingrédients faisant partie de votre sélection aparaissent ici.",
'class' => 'userIngredient select2'
)))
这是我需要做的:
在product.php中
public function getUserIngredientName()
{
return $this->userIngredients->getName(); //Issue is there is one UserIngredient for every user, I need the one of the current user
}
或形式
$builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event){
$product = $event->getData();
$form = $event->getForm();
//set the property name here but according to a custom value passed to the type
});
【问题讨论】:
-
如何创建一个 QueryBuilder 实例,根据用户成分搜索产品,然后将用户传递给表单和查询构建器?
-
是的,我可以获得要在表单中显示的正确值,但我看不到它应该应用于哪个选项?字段选项“属性”反映实体中的方法,标签用于字段而不是其选择,...?
-
我想我不明白你当时想做什么。是的,
property反映了实体方法。 -
我有一个产品类型,我试图在多方面检索一对多关系的特定实体。我正在尝试在实体产品上执行 product->getUseringredients($user) 方法。
-
您知道您已经在寻找哪种成分了吗?
标签: php forms entity-framework symfony addeventlistener