简体   繁体   English

形式上一对一的关联?

[英]One-to-one association in form?

In symfony 2.0, how to create a drop down list using one-to-one association in form? 在symfony 2.0中,如何使用表单中的一对一关联创建下拉列表? Can you guys put good example please? 你们能举好榜样吗?

I will try to answer your question the way I understand it. 我将尝试以我理解的方式回答您的问题。 Let's say I have a Faculty object bound to a single University object. 假设我有一个Faculty对象绑定到一个University对象。 So in the form used to create or edit a faculty, I display a combo box of all the university in the database and the user choose one among them. 因此,在用于创建或编辑教师的表单中,我在数据库中显示所有大学的组合框,并且用户在其中选择一个。 There is one special Symfony field type that does exactly this: the entity type. 有一个特殊的Symfony字段类型就是这样:实体类型。 Below is the code of the buildForm method that I use in my FacultyType object used to create the faculty form: 下面是我在用于创建教师表单的FacultyType对象中使用的buildForm方法的代码:

// Application\AcmeBundle\Form\Type\FacultyType
public function buildForm(FormBuilder $builder, array $options)
{
    $builder->add('name');
    $builder->add('university', 'entity', array(
        // The class of the entity used as a combo box item
        'class' => 'AcmeBundle:University',

        // The property of the entity displaying the entity as text
        'property' => 'name',

        // The query builder used to populate the combo box, accepts
        // a QueryBuilder object or a \Closure like below 
        'query_builder' => function(EntityRepository $repository) {
            // This will return a query builder selecting all universities
            return $repository->createQueryBuilder('u');
        }
    ));
}

Note: There are other properties that can be set for the entity field type, I invite you to take a look at this page for more information on it. 注意:可以为实体字段类型设置其他属性,我邀请您查看此页面以获取有关它的更多信息。

Rendered, this will show a combo box with all the universities I have set in the database. 渲染,这将显示我在数据库中设置的所有大学的组合框。 When the user save the form, the university chose is assigned to the faculty object bound to the form via a setter. 当用户保存表单时,大学选择的是通过设置器分配给绑定到表单的教师对象。 You could probably render a drop-down list instead of a combo box. 你可能会渲染一个下拉列表而不是一个组合框。 If you need to select multiple entities, the 'multiple' option of the field type entity could be useful. 如果需要选择多个实体,则字段类型实体的'multiple'选项可能很有用。

This being said, the example I showed is not a One-to-One relation but rather a Many-to-One for the Faculty object and a One-to-Many for the University object. 话虽这么说,我展示的例子不是一对一的关系,而是Faculty对象的多对一和University对象的一对多关系。 A One-to-One relation would be something more like a relation where a University has a unique Address . 一对一关系更像是University拥有唯一Address In this case, a combo box wouldn't be useful since the university can only have one adress so a sub-form would be more appropriate. 在这种情况下,组合框将没有用,因为大学只能有一个地址,因此子表单更合适。 If it has many adresses, then it becomes a One-to-Many relation like the relation between the university and its faculties. 如果它有许多地址,那么它就变成了一对多的关系,就像大学和它的院系之间的关系一样。

Not sure if this will answer your question correctly but I hope it will lead you to a final solution. 不确定这是否能正确回答您的问题,但我希望它能引导您找到最终解决方案。

Regards, Matt 问候,马特

You need to use the entity field type in Symfony2. 您需要在Symfony2中使用实体字段类型。 A good example is found at http://symfony.com/doc/current/reference/forms/types/entity.html http://symfony.com/doc/current/reference/forms/types/entity.html上有一个很好的例子

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

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