ZF2 - Model with camelCase variables and edit form

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

ZF2 - Model with camelCase variables and edit form

MichaelB
Hi all,

How to fill a form with $form->bind($mymodel) when we have "camelCase" proprety in model ?

I use many fields in database with underscore (ex: food_name, is_active, create_date, ...).
In my model in ZF2 i use camelCase variable like $foodName, $isActive, ...
In my form i use name with underscore like in the database (ex: food_name, is_active, create_date, ...)

In the function exchangeArray in my model is like below and work well:
...
$this->foodName = (isset($data['food_name'])) ? $data['food_name'] : null;      
...

Now arrive my problem.. I have a form with some fields. Is the "edit" form so data from database have to fill the form and use $form->bind($mymodel) BUT all fields with underscore DO NOT FILL because foodName != food_name.

I must use it for each of my fields to fill my edit form:
$form->get('food_name')->setAttribute('value', $mymodel->foodName);


I would like to know if is right ? Or have another solution ?


In the doc is only simple variable like id, title, ... but in majority of project have more complicate variable name.

Thanks in advance,
Michael

PS: I hope is clear explanation from my part.
Reply | Threaded
Open this post in threaded view
|

Re: ZF2 - Model with camelCase variables and edit form

Matus Zeman
Hi Michael,
Check usage of Zend\Stdlib\Hydrator's... they are supposed to do all this
magic for you.

I personally don't like when they translate camelCase to underscores by
default but that's another topic.

Cheers,
Matus


On 17 May 2013 15:47, MichaelB <[hidden email]> wrote:

> Hi all,
>
> How to fill a form with $form->bind($mymodel) when we have "camelCase"
> proprety in model ?
>
> I use many fields in database with underscore (ex: food_name, is_active,
> create_date, ...).
> In my model in ZF2 i use camelCase variable like $foodName, $isActive, ...
> In my form i use name with underscore like in the database (ex: food_name,
> is_active, create_date, ...)
>
> In the function exchangeArray in my model is like below and work well:
> ...
> $this->foodName = (isset($data['food_name'])) ? $data['food_name'] : null;
> ...
>
> Now arrive my problem.. I have a form with some fields. Is the "edit" form
> so data from database have to fill the form and use $form->bind($mymodel)
> BUT all fields with underscore DO NOT FILL because foodName != food_name.
>
> I must use it for each of my fields to fill my edit form:
> $form->get('food_name')->setAttribute('value', $mymodel->foodName);
>
>
> I would like to know if is right ? Or have another solution ?
>
>
> In the doc is only simple variable like id, title, ... but in majority of
> project have more complicate variable name.
>
> Thanks in advance,
> Michael
>
> PS: I hope is clear explanation from my part.
>
>
>
>
> --
> View this message in context:
> http://zend-framework-community.634137.n4.nabble.com/ZF2-Model-with-camelCase-variables-and-edit-form-tp4660045.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
>
> --
> List: [hidden email]
> Info: http://framework.zend.com/archives
> Unsubscribe: [hidden email]
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: ZF2 - Model with camelCase variables and edit form

graphicmist
ClassMethods implementation has underscore seperated keys method implemented. But can i use it with Array Seriliazable implementation?
Reply | Threaded
Open this post in threaded view
|

Re: ZF2 - Model with camelCase variables and edit form

Marco Pivetta
@graphicmist that depends on the implementation of `getArrayCopy` and
`exchengeArray` of the hydrated object.

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/


On 19 May 2013 12:53, graphicmist <[hidden email]> wrote:

> ClassMethods implementation has underscore seperated keys method
> implemented.
> But can i use it with Array Seriliazable implementation?
>
>
>
> --
> View this message in context:
> http://zend-framework-community.634137.n4.nabble.com/ZF2-Model-with-camelCase-variables-and-edit-form-tp4660045p4660067.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
>
> --
> List: [hidden email]
> Info: http://framework.zend.com/archives
> Unsubscribe: [hidden email]
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: ZF2 - Model with camelCase variables and edit form

MichaelB
In reply to this post by Matus Zeman
Ok thanks, i found something in that link below. Now i use all my variables in model, form, inputFilter with camelCase Like i did in general and only in Module.php i created the custom hydrator that map my fields from database (with underscore) to camelCase.. I hope is the good method, but it works!
             
// Custom Hydrator Example
$hydrator = new \Application\Hydrator\TableEntityMapper(
            array(
                'is_active' => 'isActive',
                'food_name' => 'foodName',
            )
);


See that link:
http://www.maltblue.com/tutorial/zendframework2-hydrators-models-tablegateway-pattern
Reply | Threaded
Open this post in threaded view
|

Re: ZF2 - Model with camelCase variables and edit form

graphicmist
This post was updated on .
In reply to this post by Marco Pivetta
I didnt find it a very clean approach. But your problem can be easily solved using ClassMap implementation of the Hydrator