ModelForm developments


I’ve been reworking the ModelForm class for ZF a bit. Earlier this year, I discussed porting it to use Zend_Db_Table with Matthew Weier O’Phinney, for using it with Zend Framework. I initially had done some checking on Zend_Db_Table, and some small code changes to modify the class to use it instead of Doctrine, but I ran into some issues. Now, I’ve had some time to think about it, I’ve reworked the class slightly and added basic Zend_Db_Table support, too…

The initial issues

While Zend_Db_Table supports relations, it doesn’t do them as “precisely” as Doctrine. With Doctrine, it’s very easy to determine what kind of a relation we’re dealing with, so it’s easy to generate the relation fields in the form correctly. With DbTable, this isn’t the case. There is no way to determine whether a relation is a “has one” or a “has many” without the user defining some additional properties.

Also, due to my own lack of reasoning at the time, I started porting the ModelForm class directly. I replaced some Doctrine-specific parts with Zend_Db_Table parts. Since I still wanted to support Doctrine as well, this wasn’t exactly a very good approach, and I’ve now scrapped most of this earlier code.

Solving the issues

I thought about the relations issue and the code a bit more, and I came to the conclusion that it would be best to use an adapter, similar to how Zend Framework uses adapters with certain other components.

By modifying the Doctrine-based modelform class to use an adapter, and then creating a Doctrine adapter and a Zend_Db_Table adapter, I could easily support using both of them. It would also allow anyone to add support for their favorite DB system, or even for models that aren’t directly using a DB.

As for adding parameters for the relations in Zend_Db_Table, I’ve thought about adding a new property to the modelform class, which would allow you to pass parameters to the adapter. This would allow you to tell the adapter which kind of relations it’s dealing with. Alternatively, I could modify the generator itself to accept the parameters.

I’ve already modified the modelform to use adapters, and when using the Doctrine adapter, it works exactly the same as the non-adapter version of the class. The Zend_Db_Table adapter I’m working on currently supports normal fields, but no relations.

The code can be found from the modelform-adapter branch in my svn. The code is still slightly experimental – for example, it still uses CU_Validate_DbRowExists which depends on Doctrine.

I’ve also created a proposal for adding this to Zend Framework. If you have an account on the Zend Framework wiki, I suggest you post comments about this to the proposal itself.