public function hydrate(string $klass, array $data) : EntityInterface { $entity = parent::hydrate($klass, $data); $refl = new \ReflectionClass($entity); $md = EntityMetadataRegistry::get($klass); $columns = []; foreach ($md->getColumns() as $column) { if ($md->isRelation($column->getField())) { continue; } $name = $column->getField(); $field = StringUtil::camelize($name); if (!array_key_exists($name, $data)) { $columns[$field] = new PartialEntityException($klass, $field); continue; } $columns[$field] = $column->getType()->castForEntity($data[$name]); } parent::setPrivateProperty($refl, 'columns', $entity, new AttributeCollection($entity, $columns)); $relations = []; foreach ($md->getRelations() as $name => $relation) { $relations[$name] = $relation; } parent::setPrivateProperty($refl, 'relations', $entity, new RelationCollection($entity, $relations)); return $entity; }
public function reload() { if (!$this->state === self::STATE_PERSISTED) { throw new \BadMethodCallException('Cannot reload an entity that has not been persisted'); } EntitiesConfiguration::getCache()->evict($this); $md = EntityMetadataRegistry::get(static::class); $md->reset(); $table = EntityMetadataRegistry::get(static::class)->getTable(); $sql = $table->project('*')->where($table['id']->eq($this->getId()))->getSql(); $data = EntitiesConfiguration::getConnection()->fetch($sql); if (sizeof($data) === 0) { throw ConcurrentModificationException::deleted(static::class, $this->getId()); } $data = (array) $data[0]; $columns = []; foreach ($md->getColumns() as $column) { if ($md->isRelation($column->getField())) { continue; } $name = $column->getField(); $field = StringUtil::camelize($name); if (!array_key_exists($name, $data)) { $columns[$field] = new PartialEntityException(static::class, $field); continue; } $columns[$field] = $column->getType()->castForEntity($data[$name]); } $this->columns = new AttributeCollection($this, $columns); $rels = []; foreach ($md->getRelations() as $relation) { $rels[StringUtil::camelize($relation->getColumn())] = $relation; } $this->relations = new RelationCollection($this, $rels); }