简体   繁体   English

Symfony2 参数与嵌入形式

[英]Symfony2 parameter with embedded forms

EDIT : I find the real problem!编辑:我找到了真正的问题!

I am trying to give a paramEter to a sub-form from the controller.我试图从控制器给一个子表单一个参数。 Without this parameter, the form is working perfectly.没有这个参数,表单就可以完美运行。

I want to show in the select list only users which are not already present in the relation.我想在选择列表中仅显示关系中尚未存在的用户。 I have this query_builder:我有这个 query_builder:

'query_builder' => function(UserRepository $er) use($options) {
                                    return $er->getFormateursAvailable($options['categ']);
                               },

And the method:和方法:

public function setDefaultOptions(OptionsResolverInterface  $resolver)
{
    $resolver->setDefaults(array('data_class' => 'Intranet\FormationBundle\Entity\CategorieFormateur', 'categ' => false));
    //$resolver->setDefaults(array('data_class' => 'Intranet\FormationBundle\Entity\CategorieFormateur'));
}

For the collection form, so, I have to put this option in the form:对于集合表单,因此,我必须将此选项放入表单中:

'options' => $options,

But I don't know if it is true, and I have to define the method:但是不知道是不是真的,还得定义方法:

public function setDefaultOptions(OptionsResolverInterface  $resolver)
{
    $resolver->setDefaults(array('categ' => false));
}

(The form is working without this method if there is no parameter.) And calling: (如果没有参数,表单在没有此方法的情况下工作。)并调用:

$form = $this->createForm(new GererFormateurCategorieType(), $categorie, array('categ' => $categorie));

And then, I have this error:然后,我有这个错误:

Neither the property "user" nor one of the methods "getUser()", "isUser()", "hasUser()", "__get()" exist and have public access in class "Intranet\\FormationBundle\\Entity\\Categorie".属性“user”和方法“getUser()”、“isUser()”、“hasUser()”、“__get()”都不存在并且在类“Intranet\\FormationBundle\\Entity\\Categorie”中具有公共访问权限.

The relation:关系:

Categorie has this property:类别具有此属性:

/**
* @ORM\OneToMany(targetEntity="Intranet\FormationBundle\Entity\CategorieFormateur", mappedBy="categorie", cascade={"persist", "remove"}, orphanRemoval=true)
**/
private $formateurs;

With addFormateur, removeFormateur and getFormateurs使用 addFormateur、removeFormateur 和 getFormateurs

CategorieFormateur :类别Formateur :

    /**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Intranet\FormationBundle\Entity\Categorie", inversedBy="formateurs")
* @ORM\JoinColumn(name="categorie_id", referencedColumnName="id")
*/
private $categorie;

/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Intranet\UserBundle\Entity\User")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;

with setters and getters for each properties.每个属性都有 setter 和 getter。

According to the error message, you have to add the setFormateurs() method in your Categorie entity:根据错误信息,您必须添加setFormateurs()方法在你Categorie实体:

public function setFormateurs($formateurs)
{
    $this->formateurs = $formateurs;

    return $this;
}

I found an alternative method, wich is not the true answer :我找到了另一种方法,这不是真正的答案:

In the repository, I use $request = Request::createFromGlobals();在存储库中,我使用 $request = Request::createFromGlobals();

And I explode the $requestPathInfo() to get the last parameter and I use it in the query...我分解 $requestPathInfo() 以获取最后一个参数,并在查询中使用它...

But the existing users in the relation are not loaded altough they not appers in the list.但是关系中的现有用户没有加载,尽管他们没有出现在列表中。

I hope somebody knows how to do that properly !我希望有人知道如何正确地做到这一点!

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

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