Last active
January 2, 2016 09:09
-
-
Save galvao/8280931 to your computer and use it in GitHub Desktop.
Another way to write the exchangeArray method of the Model Class in ZF2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Please read the comments below in this Gist carefully as this approach has very important caveats. | |
| namespace Application\Model; | |
| class Foo | |
| { | |
| public $id; | |
| public $bar; | |
| public $baz; | |
| public $quux; | |
| public function exchangeArray($data) | |
| { | |
| $attributes = array_keys(get_class_vars(__CLASS__)); | |
| foreach($attributes as $attribute) { | |
| if (isset($data[$attribute])) { | |
| $this->$attribute = (!empty($data[$attribute]) ? $data[$attribute] : null); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@galvao I got the first problem.
About the 2nd problem : Can't we go with is_set() ?
I was also checking out https://github.com/dhrrgn/abstract-data-layer
In that rather than keeping as property, the fields are kept in an array.