Skip to content

Instantly share code, notes, and snippets.

@iolson
Created January 22, 2015 20:48
Show Gist options
  • Save iolson/54cf4a42e5acd6793b04 to your computer and use it in GitHub Desktop.
Save iolson/54cf4a42e5acd6793b04 to your computer and use it in GitHub Desktop.
EntityPlus
<?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