Skip to content

Instantly share code, notes, and snippets.

@galvao
Last active January 2, 2016 09:09
Show Gist options
  • Select an option

  • Save galvao/8280931 to your computer and use it in GitHub Desktop.

Select an option

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
<?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);
}
}
}
}
@harikt
Copy link

harikt commented Jan 7, 2014

@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.

@galvao
Copy link
Author

galvao commented Jan 7, 2014

@harikt Ah, of course, isset would work. Been a long day of work, missed that =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment