0

I am making a ZF2 app. I am using entities, mappers and services (e.g. UserEntity, UserMapper, UserService) to manage the objects/models. Properties in the entities are CamalCased (e.g. FirstName, LastName) while in the database, fields are using underscore (first_name, last_name). I will plan to use a hydrator to map the properties and db-fields when retrieving or saving. The service object (UserService) will be used to communicate with the mapper to retrieve and save data models using the mapper. The hydrator will convert the result of mapper and convert them into proper entities.

The thing I am confused is that when the service (UserService) need to provide some cirteria - for example to find all users with a specific 'last name', will the service use the database field names (last_name) or entity properties name (LastName)?

If the db field name is used in the Service, so any change in the db structure will require me to update the service also, which completely fails the reason of using the whole approach.

J Morgan
  • 1
  • 1
  • 1
    Is there a particular hydrator that you had in mind? Some will require database column names and some won't. I do know that it's easy enough to use Doctrine 2 with ZF2. With Doctrine 2, only property names are used for queries. – Cerad Aug 26 '13 at 19:42
  • I will be using the ClassMethods hydrator. Accourding to my understanding the hydrator can extract/hydrate all the properties, and we can't use it to hydrate/extract only a few properties. – J Morgan Aug 28 '13 at 14:54

1 Answers1

0

If you take a look at the ClassMethods:hydrate method (https://github.com/zendframework/zf2/blob/master/library/Zend/Stdlib/Hydrator/ClassMethods.php) you will see that it just copies the properties from one object to another. You have the option of converting the property names to/from camelCase but that's it.

If you change a column name in your database then you will need to change corresponding property name in your object. And vice versa. Which I believe is the crux of your question?

If you want to make table column names be independent of your method names then you need something that lets you define an actual mapping table somewhere. Change a column or method name and you only need to update the configuration mapping table.

Not a ZF2 expert so I could be wrong but it doesn't look like any of the supplied hydrators support this.

I do know that Doctrine 2 supports it.

Cerad
  • 48,157
  • 8
  • 90
  • 92
  • according to my understanding the hydrator approach converts full property into db_fields and vice versa. It does provide the mapping and I want to use its mapping. so if down the road there are any db changes, I only need to update the mapping. What I want is to use the same mapping to convert only specific/provided properties/fields when needed. – J Morgan Aug 28 '13 at 19:56
  • Nope. Look at the source code. No mapping information. Just uses whatever the table column names happen to be and optionally converts them to camel case. You could probably make your own hydrator but your queries will still be tied to specific column names. Try it. – Cerad Aug 28 '13 at 20:23
  • I checked the code and do agree with you that its mainly changing/mapping the 'underscored' fields to camelCase. But we can also extend it and perform different action for any given key. e.g. if the entity has property name 'id' and the db has field name 'user_id' we can extend the class and in the extract/hydrate we will add the functionality so it convert between object and array according to our needs. Now what I want is that I use the existing functionality of the hydrator to provide the conversion for specific fields only not the whole row/object. – J Morgan Aug 29 '13 at 16:48
  • Of course you can make a custom hydrator. But will that help keeping your criteria database independent? – Cerad Aug 29 '13 at 17:16
  • thats what actually I am looking for, so that my services, entities etc don't know how the data is stored. So if a service need to provide some criteria regarding some specific property, it will query the mapper and maybe using the hydrator directly or the mapper engage the hydrator and convert the criteria according to the db structure/fields. – J Morgan Aug 29 '13 at 17:26
  • So your question is really about UserService? Are you planning on using a ZF2 class for this? If so which one? – Cerad Aug 29 '13 at 17:41