DoctrineObject, Form::setObject & Objects with defined constructors

Hi everyone! Revisiting a ZF app after a small hiatus, and I had a quick question about setObject, when the object in question has constructor arguments.

In FooFormFactory:

use DoctrineModule\Stdlib\Hydrator\DoctrineObject;

...

$form = new FooForm();
$form->setHydrator(new DoctrineObject($serviceManager->get('doctrine.entitymanager.orm_default'), false));
$form->setInputFilter($serviceManager->get('InputFilterManager')->get(FooInputFilter::class));

So far, so good.

Foo’s constructor looks like so:

public function __construct( string $domainRequirement );

This string is being punched in using the form, and at Factory time, it is unknown. This here won’t work:

$form = new FooForm();
$form->setHydrator(new DoctrineObject($serviceManager->get('doctrine.entitymanager.orm_default'), false));
$form->setInputFilter($serviceManager->get('InputFilterManager')->get(FooInputFilter::class));
$form->setObject(new Foo()); // missing ctor arg

This here works, but it feels dirty?

$form = new LemonadeInstanceForm();
$form->setHydrator(new DoctrineObject($serviceManager->get('doctrine.entitymanager.orm_default'), false));
$form->setInputFilter($serviceManager->get('InputFilterManager')->get(LemonadeInstanceInputFilter::class));

$class = new \ReflectionClass(Foo::class);
$form->setObject($class->newInstanceWithoutConstructor());

Is that how you go about it in your code? Would you go so far as to use a custom DTO in between as mediator between the form and the object to get around this?

Thanks a bunch.
Alex

How about

$form->setObject(new Foo("")); 

But for that fleeting moment, I have an invalid domain object! :wink:

yeah, I understand. suppose you had a Doctrine lifecycle event listener that will throw an exception if you try to persist or update with that empty string? So the empty string is like an IOU, a loan you have to pay back before flush() is called.

Perhaps a better idea, do the above but also revise the Foo constructor:

public function __construct( string $domainRequirement  = null);