Scalr\Model\AbstractEntity::load PHP Méthode

load() public méthode

Loads an entity from array or object
public load ( array | object $obj, string $tableAlias = null )
$obj array | object Source data
$tableAlias string optional The table alias which should be threated as column name prefix
    public function load($obj, $tableAlias = null)
    {
        $isObject = is_object($obj);
        $iterator = $this->getIterator()->fields();
        $prefix = $tableAlias ? $tableAlias . '__' : '';
        if ($prefix !== '') {
            //Input comes from the array retrieved from the database query
            foreach ($iterator as $field) {
                /* @var $field \Scalr\Model\Loader\Field */
                $name = $prefix . $field->column->name;
                if (isset($obj[$name])) {
                    $value = $obj[$name];
                } else {
                    $value = null;
                }
                $this->{$field->name} = $field->type->toPhp($value);
            }
        } else {
            foreach ($iterator as $field) {
                $name = $field->name;
                if ($isObject) {
                    $value = isset($obj->{$name}) ? $obj->{$name} : null;
                } else {
                    if (isset($obj[$field->column->name])) {
                        $value = $obj[$field->column->name];
                    } else {
                        if (isset($obj[$name])) {
                            $value = $obj[$name];
                        } else {
                            $value = null;
                        }
                    }
                }
                $this->{$name} = $field->type->toPhp($value);
            }
        }
    }

Usage Example

 /**
  * {@inheritdoc}
  * @see \Scalr\Model\AbstractEntity::load()
  */
 public function load($obj, $tableAlias = null)
 {
     parent::load($obj);
     if (empty($this->ccId) || $this->ccId === '00000000-0000-0000-0000-000000000000') {
         $this->ccId = null;
     }
     if (empty($this->projectId) || $this->projectId === '00000000-0000-0000-0000-000000000000') {
         $this->projectId = null;
     }
 }