# DoctrineObject, Form::setObject & Objects with defined constructors

**URL:** <https://discourse.laminas.dev/t/doctrineobject-form-setobject-objects-with-defined-constructors/1266>\
**Category:** Components & MVC\
**Tags:** zend-form\
**Created:** [December 9, 2019, 9:25pm UTC](https://discourse.laminas.dev/t/doctrineobject-form-setobject-objects-with-defined-constructors/1266 "2019-12-09T21:25:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saeven](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/saeven/32/100_2.png) [@Saeven](https://discourse.laminas.dev/u/Saeven)\
**Post date:** [December 9, 2019, 9:25pm UTC](https://discourse.laminas.dev/t/doctrineobject-form-setobject-objects-with-defined-constructors/1266/1 "2019-12-09T21:25:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![davidmintz](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/davidmintz/32/213_2.png) [@davidmintz](https://discourse.laminas.dev/u/davidmintz)\
**Post date:** [December 19, 2019, 3:04pm UTC](https://discourse.laminas.dev/t/doctrineobject-form-setobject-objects-with-defined-constructors/1266/2 "2019-12-19T15:04:48Z")

</div>

How about

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

```

---

<div class="post-metadata">

**Author:** ![Saeven](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/saeven/32/100_2.png) [@Saeven](https://discourse.laminas.dev/u/Saeven)\
**Post date:** [December 19, 2019, 3:34pm UTC](https://discourse.laminas.dev/t/doctrineobject-form-setobject-objects-with-defined-constructors/1266/3 "2019-12-19T15:34:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![davidmintz](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/davidmintz/32/213_2.png) [@davidmintz](https://discourse.laminas.dev/u/davidmintz)\
**Post date:** [December 19, 2019, 4:27pm UTC](https://discourse.laminas.dev/t/doctrineobject-form-setobject-objects-with-defined-constructors/1266/4 "2019-12-19T16:27:50Z")

</div>

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:

```auto
public function __construct( string $domainRequirement = null);

```
