Created
January 22, 2015 20:48
-
-
Save iolson/54cf4a42e5acd6793b04 to your computer and use it in GitHub Desktop.
EntityPlus
This file contains 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 | |
namespace AdvisorsExcel\ImportBundle\Entity; | |
/** | |
* Class EntityPlus | |
* | |
* Parent class for generic Doctrine entities that provides a convenience method for populating the entity | |
* from an associative array. | |
*/ | |
class EntityPlus | |
{ | |
/** | |
* Populates the entity using an associative array. | |
* | |
* @param array $fieldValues | |
*/ | |
function populate( $fieldValues ) | |
{ | |
foreach ( $fieldValues as $field => $value ) { | |
if ( strpos( $field, '_' ) !== false ) { | |
$fields = explode( '_', $field ); | |
$field = $fields[0]; | |
$i = 0; | |
foreach ( $fields as $f ) { | |
if ( $i != 0 ) { | |
$field .= ucfirst( $f ); | |
} | |
$i ++; | |
} | |
} | |
$method = 'set' . ucfirst( $field ); | |
$this->$method( $value ); // NOTE: No error checking. | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment