- Still WIP on removing annotations
This commit is contained in:
parent
62792e8325
commit
d9fa51fa18
|
@ -4,7 +4,7 @@ namespace Ulmus\Entity\Sqlite;
|
|||
|
||||
use Ulmus\EntityCollection;
|
||||
use Ulmus\Query\{From, Select};
|
||||
use Ulmus\{Attribute\Obj\Table, Repository};
|
||||
use Ulmus\{Attribute\Obj\Table, Repository, Ulmus};
|
||||
use Ulmus\Attribute\Property\{Field, Filter, FilterJoin, Relation, Join, Virtual, Where};
|
||||
|
||||
#[Table(name: "sqlite_master")]
|
||||
|
@ -27,12 +27,14 @@ class Schema
|
|||
#[Field]
|
||||
public ? string $sql;
|
||||
|
||||
##[Relation("oneToMany", key: "tableName", foreignKey: "tableName", entity: Column::class)]
|
||||
#[Virtual(method: "filterColumns")]
|
||||
public EntityCollection $columns;
|
||||
|
||||
public function filterColumns() : EntityCollection
|
||||
{
|
||||
return ( new Repository\SqliteRepository(Column::class) )->pragma('table_info', $this->tableName)->collectionFromQuery();
|
||||
$adapter = Ulmus::$registeredAdapters[$this->loadedFromAdapter];
|
||||
|
||||
return Column::repository(Repository\SqliteRepository::DEFAULT_ALIAS, $adapter)
|
||||
->pragma('table_info', $this->tableName)->collectionFromQuery();
|
||||
}
|
||||
}
|
|
@ -3,6 +3,7 @@
|
|||
namespace Ulmus\Entity\Sqlite;
|
||||
|
||||
use Ulmus\ConnectionAdapter;
|
||||
use Ulmus\EntityCollection;
|
||||
use Ulmus\Repository;
|
||||
|
||||
#[\Ulmus\Attribute\Obj\Table(name: "sqlite_master")]
|
||||
|
@ -10,12 +11,16 @@ class Table extends Schema
|
|||
{
|
||||
public static function repository(string $alias = Repository::DEFAULT_ALIAS, ConnectionAdapter $adapter = null): Repository
|
||||
{
|
||||
return new class(static::class, $alias, $adapter) extends Repository\SqliteRepository
|
||||
$repository = new class(static::class, $alias, $adapter) extends Repository\SqliteRepository
|
||||
{
|
||||
public function finalizeQuery(): void
|
||||
{
|
||||
$this->select(Table::field('tableName'))->groupBy(Table::field('tableName'));
|
||||
}
|
||||
};
|
||||
|
||||
$repository->entityClass = static::class;
|
||||
|
||||
return $repository;
|
||||
}
|
||||
}
|
|
@ -14,6 +14,9 @@ use Ulmus\{Common\EntityResolver,
|
|||
trait EntityTrait {
|
||||
use EventTrait;
|
||||
|
||||
#[Ignore]
|
||||
public string $loadedFromAdapter;
|
||||
|
||||
#[Ignore]
|
||||
protected bool $entityStrictFieldsDeclaration = false;
|
||||
|
||||
|
@ -324,7 +327,6 @@ trait EntityTrait {
|
|||
#[Ignore]
|
||||
public static function field($name, null|string|false $alias = Repository::DEFAULT_ALIAS) : EntityField
|
||||
{
|
||||
|
||||
$default = ( $alias === false ? '' : Repository::DEFAULT_ALIAS ); # bw compatibility, to be deprecated
|
||||
|
||||
return new EntityField(static::class, $name, $alias ? Ulmus::repository(static::class)->adapter->adapter()->escapeIdentifier($alias, Adapter\AdapterInterface::IDENTIFIER_FIELD) : $default, Ulmus::resolveEntity(static::class));
|
||||
|
|
|
@ -372,14 +372,12 @@ class Repository
|
|||
|
||||
public function yield() : \Generator
|
||||
{
|
||||
$class = $this->entityClass;
|
||||
|
||||
$this->selectSqlQuery();
|
||||
|
||||
$this->finalizeQuery();
|
||||
|
||||
foreach(Ulmus::iterateQueryBuilder($this->queryBuilder, $this->adapter) as $entityData) {
|
||||
yield ( new $class() )->entityFillFromDataset($entityData);
|
||||
yield $this->instanciateEntity()->entityFillFromDataset($entityData);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -698,7 +696,7 @@ class Repository
|
|||
}
|
||||
}
|
||||
|
||||
if ( ! in_array(WithOptionEnum::SkipJoinFilter, $options) ) {
|
||||
if ( ! in_array(WithOptionEnum::SkipJoinFilter, $options) ) {
|
||||
foreach ($this->entityResolver->searchFieldAnnotationList($item, [Attribute\Property\FilterJoin::class, FilterJoin::class]) as $filter) {
|
||||
call_user_func_array([$this->entityClass, $filter->method], [$join, $item, true]);
|
||||
}
|
||||
|
@ -862,14 +860,17 @@ class Repository
|
|||
|
||||
public function collectionFromQuery(? string $entityClass = null) : EntityCollection
|
||||
{
|
||||
$class = $entityClass ?? $this->entityClass;
|
||||
$entityClass ??= $this->entityClass;
|
||||
|
||||
$entityCollection = $class::entityCollection();
|
||||
$entityCollection = $entityClass::entityCollection();
|
||||
|
||||
$this->finalizeQuery();
|
||||
|
||||
foreach(Ulmus::iterateQueryBuilder($this->queryBuilder, $this->adapter) as $entityData) {
|
||||
$entityCollection->append( ( new $class() )->resetVirtualProperties()->entityFillFromDataset($entityData) );
|
||||
$entity = $this->instanciateEntity($entityClass);
|
||||
$entity->loadedFromAdapter = $this->adapter->name;
|
||||
|
||||
$entityCollection->append( $entity->resetVirtualProperties()->entityFillFromDataset($entityData) );
|
||||
}
|
||||
|
||||
$this->eventExecute(Event\Repository\CollectionFromQueryInterface::class, $entityCollection);
|
||||
|
@ -1042,9 +1043,11 @@ class Repository
|
|||
return $this->entityClass::entityCollection(...$arguments);
|
||||
}
|
||||
|
||||
public function instanciateEntity() : object
|
||||
public function instanciateEntity(? string $entityClass = null) : object
|
||||
{
|
||||
return new $this->entityClass();
|
||||
$entityClass ??= $this->entityClass;
|
||||
|
||||
return new $entityClass;
|
||||
}
|
||||
|
||||
public function hasFilters() : bool
|
||||
|
@ -1053,7 +1056,7 @@ class Repository
|
|||
}
|
||||
|
||||
protected function matchEntity(object $entity) {
|
||||
return get_class($entity) === $this->entityClass;
|
||||
return $entity::class === $this->entityClass;
|
||||
}
|
||||
|
||||
protected function finalizeQuery() : void {}
|
||||
|
|
|
@ -89,7 +89,7 @@ class RelationBuilder
|
|||
}
|
||||
}
|
||||
catch(\Throwable $e) {
|
||||
throw new $e(sprintf("An error occurred while calling method from your #[Virtual] attribute in entity '%s::%s'.", $this->entity::class, $name ));
|
||||
throw new $e(sprintf("An error occurred while calling method from your #[Virtual] attribute in entity '%s::%s'. Caught '%s'", $this->entity::class, $name, $e->getMessage() ));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue