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 getChanges() : array { $md = EntityMetadataRegistry::get(get_class(parent::getEntity())); $changes = []; foreach (parent::getChanges() as $key => $value) { $column = StringUtil::underscore($key); $relation = $md->getRelation($column); // If the column isn't a relation, or the relation isn't defined by a column on the current entity, we // can skip it if ($relation === null || $md->getColumn($column) === null) { continue; } if ($value instanceof EntityInterface) { $value = $value->getId(); } $changes[$key] = $value; } return $changes; }
private static function __load() { if (EntityMetadataRegistry::has(static::class)) { return; } EntityMetadataRegistry::add(new EntityMetadata(static::class)); static::__init(); }