从字段集 ZF2 中水化多个对象 [英] hydrating multiple objects from fieldsets ZF2

查看:27
本文介绍了从字段集 ZF2 中水化多个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在为 ZF2 中的对象补水时遇到问题.

I have a problem with hydrating the objects in ZF2.

我有一个表格可以将组织相关信息或个人相关信息保存到数据库中.用户做出选择:保存组织或个人.所有 html 输入只有 1 个表单.

I have a form for saving either the organization-related info or person-related info into the database. The user makes the choice: save either organization or person. Just 1 form for all html inputs.

在 ZF2 中,我创建了 2 个字段集.在每个字段集类中,我使用 setHydrator(new ClassMethods(false))setObject(new ).这两个字段集包含在表单中,代码如下:

In ZF2, I created 2 fieldsets. In each fieldset class, I use setHydrator(new ClassMethods(false)) and setObject(new <objectForHydration>). The 2 fieldsets are included into the form with the following code:

    $this->add([
        'type' => 'Parties\Form\Fieldsets\RegisterOrganizationFieldset',
        'options' => [
            'use_as_base_fieldset' => true,
        ],
    ]);

    $this->add([
        'type' => 'Parties\Form\Fieldsets\RegisterPersonFieldset',
        'options' => [
            'use_as_base_fieldset' => true,
        ],
    ]);

我希望 RegisterOrganizationFieldsetOrganizationObject 进行水合,RegisterPersonFieldsetPersonObject 进行水合.

I want RegisterOrganizationFieldset to hydrate OrganizationObject, and RegisterPersonFieldset to hydrate PersonObject.

我想同时对两个对象进行补水,因为这不会在表单类中引入条件.问题是水合只发生 1 个对象,具体取决于哪个字段集具有 use_as_base_fieldset = true.如果两个字段集都具有 use_as_base_fieldset = true,则代码中稍后的字段集 (RegisterPersonFieldset) 仅水合其各自的对象.

I thought of hydrating both objects at the same time because this won't introduce conditionals into the form class. The problem is the hydration takes place just 1 object depending on which fieldset has use_as_base_fieldset = true. If both fieldsets have use_as_base_fieldset = true, the fieldset later in the code (RegisterPersonFieldset) hydrates only its respective object.

你能告诉我如何给这两个物体补水吗?也许如何以更好的方式为物体补水?

Could you tell how to hydrate both objects? Maybe how to hydrate objects in a better way?

正如@jcropp 所指出的,我使用 Person 和 Organization 作为当事方,它们是仅共享 Id 属性的独立实体.

As @jcropp pointed, I use Person and Organization as Parties that are independent entities sharing only Id property.

推荐答案

同时水合两个对象的一种方法是使它们成为同一个对象.

One way to hydrate two objects at the same time is to make them both the same object.

从你的问题中不清楚是1)注册的人是注册组织的成员,还是2)当事人"正在注册,当事人可以是组织也可以是个人.以下是这两种情况的一些答案:

It is not clear from your question whether 1) a person who is registering is a member of a registering organization, or 2) "parties" are registering, and a party can be either an organization or a person. Here are some answers for both cases:

如果人员是您的数据结构中的组织成员,则同时为两者加水的方法是将人员数据视为组织数据元素的集合".此方法利用字段集之间的关系来创建单个数据集,其中组织数据和个人数据可以一起编辑.请参阅 ZF2 手册.

If persons are members of organizations in your data structure, a method for hydrating both at the same time is to treat the persons data as "collections" of the organization data elements. This method utilizes relationships between the fieldsets to create a single dataset in which both the organization data and the person data can be edited together. See the ZF2 Manual.

如果您的目的是注册单个政党,而一个政党可以是组织或个人,那么以相同的形式对一个或另一个进行补水的方法是使用表继承.在单表继承中,您将为两种类型的各方提供一个表(和一个字段集),并且在人员或组织类型之间进行选择将指示数据应输入到表的组织字段还是人员字段中.在多表继承中,您将创建一个与组织表和人员表相关的派对表;组织和个人字段集将扩展派对字段集.

If your intent is to register individual parties, and a party can be either an organization or a person, a method for hydrating one or the other in the same form is to use table inheritance. In single table inheritance you would have a single table (and a single fieldset) for both types of parties, and a selection between the person or organization type would indicate whether data should be entered into the organization fields or the person fields of the table. In multiple table inheritance, you would create a party table that related to an organization table and to a person table; and organization and person fieldsets would extend the party fieldset.

这篇关于从字段集 ZF2 中水化多个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆