June 23rd, 2014 · php zend framework 2 doctrine orm

Zend\Form, Nonuniform Collections and You

I'm a fan of, in no particular order, ZF2's Form component, Doctrine ORM and classy service layers. What can I say? My brain runs on PHP, but my heart belongs with Java 😛.

One of the things I like most about Doctrine ORM is Inheritance Mapping — more specifically, Class Table Inheritance. Since you've made it this far I won't bore you with the details, except to say it allows you to build a ToMany association to a group of related entity classes and treat instances as a single collection. I think that's awesome...unfortunately Zend\Form does not: it's Collection class will only accept a single Fieldset prototype.

So, what did I do? I hacked it, and the result is LdcZendFormCTI (rolls right off the tongue, it does). Install it via Composer:

composer require adamlundrigan/ldc-zend-form-cti:@stable

And now your form collection elements can have super more-than-just-one-fieldset-prototype powers too! Just add an instanceof NonuniformCollection to a fieldset, populate it with a prototype fieldset+hydrator+object matching each child entity in the inheritance scheme, and Bob's your uncle! There's also a NonuniformCollectionInputFilter for configuring filtering and validation, if you're into that sort of thing.

It's a little light on documentation at present, but you can get the gist of how to use it from the bundled tests. I'm working on a README and a simple demo application to show how it all fits together, both of which should be ready soonish.