Compare commits

..

No commits in common. "8f9332fbad72b9c372a5c7c2b3f7e7ca67a0ebf9" and "b76f6ff9d4a3ebd41f016533be58000a6b9f86f5" have entirely different histories.

17 changed files with 73 additions and 192 deletions

View File

@ -14,7 +14,7 @@ class Field implements \Ulmus\Annotation\Annotation {
public array $attributes = [];
public bool $nullable;
public bool $nullable = false;
public function __construct(? string $type = null, ? int $length = null)
{

View File

@ -1,11 +0,0 @@
<?php
namespace Ulmus\Annotation\Property\Field;
class Date extends \Ulmus\Annotation\Property\Field {
public function __construct(? string $type = "date", ? int $length = null)
{
parent::__construct($type, $length);
}
}

View File

@ -1,11 +0,0 @@
<?php
namespace Ulmus\Annotation\Property\Field;
class Datetime extends \Ulmus\Annotation\Property\Field {
public function __construct(? string $type = "datetime", ? int $length = null)
{
parent::__construct($type, $length);
}
}

View File

@ -1,11 +0,0 @@
<?php
namespace Ulmus\Annotation\Property\Field;
class Time extends \Ulmus\Annotation\Property\Field {
public function __construct(? string $type = "time", ? int $length = null)
{
parent::__construct($type, $length);
}
}

View File

@ -1,5 +0,0 @@
<?php
namespace Ulmus\Annotation\Property;
class Having extends Where {}

View File

@ -1,5 +0,0 @@
<?php
namespace Ulmus\Annotation\Property;
class On extends Where {}

View File

@ -20,8 +20,6 @@ class Relation implements \Ulmus\Annotation\Annotation {
public string $entity;
public string $join;
public string $function = "loadAll";
public function __construct(string $type = null)
@ -46,9 +44,4 @@ class Relation implements \Ulmus\Annotation\Annotation {
return new $e();
}
public function normalizeType() : string
{
return strtolower(str_replace(['-', '_', ' '], '', $this->type));
}
}

View File

@ -6,7 +6,7 @@ use Ulmus\Query;
class Where implements \Ulmus\Annotation\Annotation {
public /* stringable */ $field;
public string $field;
public $value;

View File

@ -64,7 +64,7 @@ class EntityField
public static function isObjectType($type) : bool
{
# @Should be fixed with isBuiltIn() instead, it won't be correct based only on name
# @ Should be fixed with isBuiltIn() instead, it won't be correct based only on name
# return strpos($type, "\\") !== false;
}

View File

@ -5,4 +5,5 @@ namespace Ulmus\Entity\Field;
class Time extends Datetime {
public string $format = "H:i:s";
}

View File

@ -8,8 +8,8 @@ use Ulmus\Repository,
Ulmus\Common\EntityField;
use Ulmus\Annotation\Classes\{ Method, Table, Collation, };
use Ulmus\Annotation\Property\{ Field, Filter, Relation, OrderBy, Where, Join, Virtual, On, };
use Ulmus\Annotation\Property\Field\{ Id, ForeignKey, CreatedAt, UpdatedAt, Datetime as DateTime, Date, Time, };
use Ulmus\Annotation\Property\{ Field, Filter, Relation, OrderBy, Where, Join, Virtual };
use Ulmus\Annotation\Property\Field\{ Id, ForeignKey, CreatedAt, UpdatedAt, };
trait EntityTrait {
use EventTrait;
@ -40,54 +40,46 @@ trait EntityTrait {
# @TODO REFACTOR THIS CODE ASAP !
if ( $this->isLoaded() ) {
$annotation = $entityResolver->searchFieldAnnotation($name, new Annotation\Property\Join) ?:
$entityResolver->searchFieldAnnotation($name, new Annotation\Property\Relation);
if ( $annotation ) {
if ( null !== ( $join= $entityResolver->searchFieldAnnotation($name, new Join() ) ) ) {
$vars = [];
$len = strlen( $name ) + 1;
$entity = $join->entity ?? $entityResolver->properties[$name]['type'];
foreach($this->entityDatasetUnmatchedFields as $key => $value) {
$len = strlen( $name ) + 1;
if ( substr($key, 0, $len ) === "{$name}$" ) {
$vars[substr($key, $len)] = $value;
}
}
if ( [] !== $data = (array_values(array_unique($vars)) !== [ null ] ? $vars : []) ) {
$entity = $annotation->entity ?? $entityResolver->properties[$name]['type'];
return $this->$name = ( new $entity() )->fromArray($data)->resetVirtualProperties();
return ( new $entity() )->fromArray($data);
}
}
if ( null !== ( $relation = $entityResolver->searchFieldAnnotation($name, new Relation() ) ) ) {
$relationType = strtolower(str_replace(['-', '_', ' '], '', $relation->type));
$order = $entityResolver->searchFieldAnnotationList($name, new OrderBy() );
$where = $entityResolver->searchFieldAnnotationList($name, new Where() );
$filters = $entityResolver->searchFieldAnnotationList($name, new Filter() );
$baseEntity = $relation->entity ?? $relation->bridge ?? $entityResolver->properties[$name]['type'];
$repository = $baseEntity::repository()->open();
if ( $relation->entity ?? false ) {
$baseEntity = $relation->entity();
$repository = $baseEntity->repository();
foreach($where as $condition) {
$repository->where($condition->field, is_callable($condition->value) ? call_user_func_array($condition->value, [ $this ]) : $condition->value, $condition->operator, $condition->condition);
}
$repository->close();
foreach($order as $item) {
$repository->orderBy($item->field, $item->order);
}
$applyFilter = function($repository) use ($filters, $name) {
foreach($filters as $filter) {
$repository = call_user_func_array([ $this, $filter->method ], [ $repository, $name ]);
}
return $repository;
};
$field = $relation->key;
}
$applyFilter = function($repository) use ($filters, $name) {
foreach($filters as $filter) {
@ -97,12 +89,10 @@ trait EntityTrait {
return $repository;
};
switch( $relation->normalizeType() ) {
switch( $relationType ) {
case 'onetoone':
$repository->limit(1);
if ($relation->foreignKey) {
$repository->where( is_object($relation->foreignKey) ? $relation->foreignKey : $baseEntity::field($relation->foreignKey), is_callable($field) ? $field($this) : $this->$field );
$repository->where( is_object($relation->foreignKey) ? $relation->foreignKey : $baseEntity->field($relation->foreignKey), is_callable($field) ? $field($this) : $this->$field );
}
$this->eventExecute(Event\EntityRelationLoadInterface::class, $name, $repository);
@ -110,14 +100,14 @@ trait EntityTrait {
$result = call_user_func([$repository, $relation->function]);
if ( count($result) === 0 ) {
return new $baseEntity();
return $baseEntity;
}
return $this->$name = $result[0];
case 'onetomany':
if ($relation->foreignKey) {
$repository->where( is_object($relation->foreignKey) ? $relation->foreignKey : $baseEntity::field($relation->foreignKey), is_callable($field) ? $field($this) : $this->$field );
$repository->where( is_object($relation->foreignKey) ? $relation->foreignKey : $baseEntity->field($relation->foreignKey), is_callable($field) ? $field($this) : $this->$field );
}
$this->eventExecute(Event\EntityRelationLoadInterface::class, $name, $repository);
@ -148,14 +138,10 @@ trait EntityTrait {
->join(Query\Join::TYPE_INNER, $this->resolveEntity()->tableName(), $relation->bridge::field($bridgeRelation->key, $bridgeAlias), static::field($bridgeRelation->foreignKey, $relationAlias), $relationAlias)
->where( static::field($bridgeRelation->foreignKey, $relationAlias), $this->{$bridgeRelation->foreignKey} );
$repository->open();
foreach($where as $condition) {
$repository->where($condition->field, $condition->value, $condition->operator);
}
$repository->close();
foreach($order as $item) {
$repository->orderBy($item->field, $item->order);
}
@ -172,14 +158,10 @@ trait EntityTrait {
->join(Query\Join::TYPE_INNER, $this->resolveEntity()->tableName(), $relation->bridge::field($bridgeRelation->key, $bridgeAlias), static::field($bridgeRelation->foreignKey, $relationAlias), $relationAlias)
->where( static::field($bridgeRelation->foreignKey, $relationAlias), $this->{$bridgeRelation->foreignKey} );
$repository->open();
foreach($where as $condition) {
$repository->where($condition->field, $condition->value, $condition->operator);
}
$repository->close();
foreach($order as $item) {
$repository->orderBy($item->field, $item->order);
}
@ -204,9 +186,6 @@ trait EntityTrait {
*/
public function __isset(string $name) : bool
{
#if ( null !== $relation = static::resolveEntity()->searchFieldAnnotation($name, new Relation() ) ) {
# return isset($this->{$relation->key});
#}
if ( $this->isLoaded() && static::resolveEntity()->searchFieldAnnotation($name, new Relation() ) ) {
return true;
}
@ -217,7 +196,7 @@ trait EntityTrait {
/**
* @Ignore
*/
public function entityFillFromDataset(iterable $dataset) : self
public function entityFillFromDataset(iterable $dataset, bool $isLoadedDataset = false) : self
{
$loaded = $this->isLoaded();
@ -258,9 +237,6 @@ trait EntityTrait {
$value = substr($value, 0, $annotation->length);
}
}
elseif ( $field['type'] === 'bool' ) {
$this->{$field['name']} = (bool) $value;
}
$this->{$field['name']} = $value;
}
@ -332,10 +308,6 @@ trait EntityTrait {
$dataset[$realKey] = Ulmus::encodeArray($this->$key);
break;
case is_bool($this->$key):
$dataset[$realKey] = (int) $this->$key;
break;
default:
$dataset[$realKey] = $this->$key;
}
@ -345,7 +317,6 @@ trait EntityTrait {
}
}
# @TODO Must fix recursive bug !
if ($includeRelations) {
foreach($entityResolver->properties as $name => $field){
$relation = $entityResolver->searchFieldAnnotation($name, new Relation() );

View File

@ -3,7 +3,6 @@
namespace Ulmus\Migration;
use Ulmus\Annotation\Property\Field;
use Ulmus\Entity;
class FieldDefinition {
@ -29,6 +28,7 @@ class FieldDefinition {
$this->builtIn = $data['builtin'];
$this->tags = $data['tags'];
$field = $this->getFieldTag();
$this->type = $field->type ?? $data['type'];
$this->length = $field->length ?? null;
@ -45,19 +45,8 @@ class FieldDefinition {
public function getSqlType(bool $typeOnly = false) : string
{
$type = $this->type;
$length = $this->length;
if ( is_a($type, Entity\Field\Date::class, true) ) {
$type = "DATE";
}
elseif ( is_a($type, Entity\Field\Time::class, true) ) {
$type = "TIME";
}
elseif ( is_a($type, \DateTime::class, true) ) {
$type = "DATETIME";
}
switch($type) {
case "bool":
$type = "TINYINT";

View File

@ -37,8 +37,6 @@ class Join extends Fragment
public /* QueryBuilder */ $queryBuilder;
public int $order = 40;
public function __construct(QueryBuilder $queryBuilder)
{
$this->queryBuilder = new QueryBuilder();
@ -49,14 +47,18 @@ class Join extends Fragment
{
$this->side = $side;
$this->table = $table;
$this->where($this->field = $field, $this->value = $value);
$this->field = $field;
$this->value = $value;
}
public function render() : string
{
if ($this->queryBuilder->where ?? false ) {
$where = $this->renderSegments([Where::CONDITION_AND, $this->queryBuilder->render(true)]);
}
return $this->renderSegments([
strtoupper($this->side), $this->outer ? static::SQL_OUTER : "", static::SQL_TOKEN, $this->table, $this->alias ?? "", $this->attachment, $this->queryBuilder->render(true) ?? ""
strtoupper($this->side), $this->outer ? static::SQL_OUTER : "", static::SQL_TOKEN, $this->table, $this->alias ?? "", $this->attachment, $this->field, "=", $this->value, $where ?? ""
]);
}
}

View File

@ -191,7 +191,7 @@ class QueryBuilder
return $this;
}
public function where(/* stringable*/ $field, $value, string $operator = Query\Where::OPERATOR_EQUAL, string $condition = Query\Where::CONDITION_AND, bool $not = false) : self
public function where($field, $value, string $operator = Query\Where::OPERATOR_EQUAL, string $condition = Query\Where::CONDITION_AND, bool $not = false) : self
{
# Empty IN case
if ( [] === $value ) {

View File

@ -2,7 +2,6 @@
namespace Ulmus;
use Ulmus\Annotation\Property\{ Where, Having, Relation, Join };
use Ulmus\Common\EntityResolver;
class Repository
@ -438,59 +437,27 @@ class Repository
return $this->where($primaryKeyField[$pkField]->name ?? $pkField, $value);
}
public function withJoin(/*string|array*/ $fields) : self
{
if ( null === $this->queryBuilder->getFragment(Query\Select::class) ) {
$this->select("{$this->alias}.*");
}
# Apply FILTER annotation to this too !
foreach((array) $fields as $item) {
$annotation = $this->entityResolver->searchFieldAnnotation($item, new Join) ?:
$this->entityResolver->searchFieldAnnotation($item, new Relation);
if ( null !== $join = $this->entityResolver->searchFieldAnnotation($item, new Annotation\Property\Join) ) {
$alias = $join->alias ?? $item;
if (( $annotation instanceof Relation ) && ( $annotation->normalizeType() === 'manytomany' )) {
throw new Exception("Many-to-many relation can not be preloaded within joins.");
}
if ( $annotation ) {
$alias = $annotation->alias ?? $item;
$entity = $annotation->entity ?? $this->entityResolver->properties[$item]['type'];
$entity = $join->entity ?? $this->entityResolver->properties[$item]['type'];
foreach($entity::resolveEntity()->fieldList(Common\EntityResolver::KEY_COLUMN_NAME, true) as $key => $field) {
$this->select("$alias.$key as {$alias}\${$field['name']}");
}
$this->open();
$key = is_string($join->key) ? $this->entityClass::field($join->key) : $join->key;
$foreignKey = is_string($join->foreignKey) ? $entity::field($join->foreignKey, $alias) : $join->foreignKey;
foreach($this->entityResolver->searchFieldAnnotationList($item, new Where() ) as $condition) {
if ( is_object($condition->field) && ( $condition->field->entityClass !== $entity ) ) {
$this->where(is_object($condition->field) ? $condition->field : $entity::field($condition->field), $condition->value, $condition->operator);
}
}
foreach($this->entityResolver->searchFieldAnnotationList($item, new Having() ) as $condition) {
$this->having(is_object($condition->field) ? $condition->field : $entity::field($condition->field), $condition->value, $condition->operator);
}
$this->close();
$key = is_string($annotation->key) ? $this->entityClass::field($annotation->key) : $annotation->key;
$foreignKey = is_string($annotation->foreignKey) ? $entity::field($annotation->foreignKey, $alias) : $annotation->foreignKey;
$this->join("LEFT", $entity::resolveEntity()->tableName(), $key, $foreignKey, $alias, function($join) use ($item, $entity, $alias) {
foreach($this->entityResolver->searchFieldAnnotationList($item, new Where() ) as $condition) {
$field = clone $condition->field;
if ( is_object($condition->field) && ( $condition->field->entityClass === $entity ) ) {
$field->alias = $alias;
$join->where(is_object($field) ? $field : $entity::field($field, $alias), $condition->value, $condition->operator);
}
}
});
$this->join($join->type, $entity::resolveEntity()->tableName(), $key, $foreignKey, $alias);
}
else {
throw new \Exception("You referenced field `$item` which do not exist or do not contain a valid @Join annotation.");
@ -500,6 +467,7 @@ class Repository
return $this;
}
public function filterServerRequest(SearchRequest\SearchRequestInterface $searchRequest) : self
{
$searchRequest->count = $searchRequest->filter( clone $this )