【问题标题】:symfony entity : use another entity attribute in a property/to_string methodsymfony entity : 在 property/to_string 方法中使用另一个实体属性
【发布时间】: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


【解决方案1】:

我刚刚从这个拉取请求中发现了新功能choice_value 可以让我做到这一点。然后我可以将我的产品传递给一个函数,并为当前用户获取相应的 userIngredient 值。

拉取请求: https://github.com/symfony/symfony/pull/12148

还不知道这是否真的被合并到 2.7.有人知道吗?

【讨论】:

    【解决方案2】:

    检查完您的 cmets 后,我将发布此解决方案,看看它是否适合您。您可以简单地将所需的集合传递给构造函数中的表单本身。如果您愿意,也可以将其传递给 $options 数组。

    控制器:

    // do whatever you need to grab the entities you want
    $em = $this->getDoctrine()->getManager();
    
    $productChoices = $em->getRepository('YourBundle:Product')
        ->getUserIngredients();
    
    // if the form is tied to an entity, pass as the 2nd parameter
    $form = $this->createForm(new YourFormType($productChoices));
    

    表单类型:

    private $productChoices;
    
    public function __construct(array $productChoices)
    {
        $this->productChoices = $productChoices;
    }
    
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('product', 'genemu_jqueryselect2_entity', array(
                'choices' => $this->choices,
                // ...
            ))
        ;
    }
    

    【讨论】:

    • 嗯,可能是解决方案。对于典型的选择字段,数组类似于 array('choice.id'=>'choice.displayedText')。在这里,我需要类似 ('product.id'=>'product.userIngredients(User)->getName()') 的东西。问题是对于实体而言,选择数组必须是实体数组,不是吗?那么如何同时传递产品 id 和 useringredient Name 呢?
    • 如果你必须计算它,你可以创建一个变量氧化物类来存储你想要的值,然后在传递给表单之前设置它。然后只需在该属性上为您的表单使用 getter。
    猜你喜欢
    • 2020-01-13
    • 1970-01-01
    • 1970-01-01
    • 2014-12-03
    • 2013-03-28
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    • 2021-05-30
    相关资源
    最近更新 更多