I have a problem with hydrating the objects in ZF2.
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.
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,
],
]);
I want RegisterOrganizationFieldset
to hydrate OrganizationObject
, and RegisterPersonFieldset
to hydrate PersonObject
.
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?
EDIT:
As @jcropp pointed, I use Person and Organization as Parties that are independent entities sharing only Id property.