Compare commits
No commits in common. "7dd64abf294212365488e3d6283df3c5e8698885" and "f74d1907d94facc29ba227044e9ec9aad80a8431" have entirely different histories.
7dd64abf29
...
f74d1907d9
@ -11,7 +11,7 @@ class Id extends \Ulmus\Annotation\Property\Field {
|
||||
$this->attributes['primary_key'] = true;
|
||||
$this->attributes['auto_increment'] = true;
|
||||
|
||||
parent::__construct('bigint');
|
||||
parent::__construct('int');
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,15 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Ulmus\Annotation\Property;
|
||||
|
||||
class Filter implements \Ulmus\Annotation\Annotation {
|
||||
|
||||
public string $method;
|
||||
|
||||
public function __construct(string $method = null)
|
||||
{
|
||||
if ( $method !== null ) {
|
||||
$this->method = $method;
|
||||
}
|
||||
}
|
||||
}
|
@ -12,9 +12,7 @@ class Where implements \Ulmus\Annotation\Annotation {
|
||||
|
||||
public string $operator;
|
||||
|
||||
public string $condition;
|
||||
|
||||
public function __construct(? string $field = null, $value = null, ? string $operator = null, ? string $condition = null)
|
||||
public function __construct(? string $field = null, $value = null, ? string $operator = null)
|
||||
{
|
||||
if ( $field !== null ) {
|
||||
$this->field = $field;
|
||||
@ -24,7 +22,11 @@ class Where implements \Ulmus\Annotation\Annotation {
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
$this->operator = $operator !== null ? $operator : Query\Where::OPERATOR_EQUAL;
|
||||
$this->condition = $condition !== null ? $condition : Query\Where::CONDITION_AND;
|
||||
if ( $operator !== null ) {
|
||||
$this->operator = $operator;
|
||||
}
|
||||
else {
|
||||
$this->operator = Query\Where::OPERATOR_EQUAL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -26,11 +26,11 @@ class EntityCollection extends \ArrayObject {
|
||||
}
|
||||
}
|
||||
|
||||
public function filtersCollection(Callable $callback, bool $replaceCollection = false) : self
|
||||
public function filtersCollection(Callable $callback, bool $yieldValueOnly = false, bool $replaceCollection = false) : self
|
||||
{
|
||||
$collection = new static();
|
||||
|
||||
foreach($this->filters($callback, true) as $item) {
|
||||
foreach($this->filters($callback, $yieldValueOnly) as $item) {
|
||||
$collection->append($item);
|
||||
}
|
||||
|
||||
@ -78,13 +78,6 @@ class EntityCollection extends \ArrayObject {
|
||||
return $removed;
|
||||
}
|
||||
|
||||
public function clear() : self
|
||||
{
|
||||
$this->exchangeArray([]);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function search($value, string $field, bool $strict = true) : Generator
|
||||
{
|
||||
foreach($this->filters(fn($v) => $strict ? $v->$field === $value : $v->$field == $value) as $key => $item) {
|
||||
@ -113,11 +106,6 @@ class EntityCollection extends \ArrayObject {
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public function searchInstances(string $className) : self
|
||||
{
|
||||
return $this->filtersCollection(fn($obj) => is_a($obj, $className));
|
||||
}
|
||||
|
||||
public function column($field, bool $unique = false) : array
|
||||
{
|
||||
$list = [];
|
||||
@ -130,8 +118,8 @@ class EntityCollection extends \ArrayObject {
|
||||
$value = $item->$field;
|
||||
}
|
||||
|
||||
if ($unique && in_array($value, $list, true)) {
|
||||
continue;
|
||||
if ($unique && in_array($value, $list)) {
|
||||
break;
|
||||
}
|
||||
|
||||
$list[] = $value;
|
||||
@ -233,49 +221,26 @@ class EntityCollection extends \ArrayObject {
|
||||
|
||||
public function fromArray(array $datasets, ? string /*stringable*/ $entityClass = null) : self
|
||||
{
|
||||
|
||||
if ( ! ($this->entityClass || $entityClass) ) {
|
||||
throw new \Exception("An entity class name must be provided to be instanciated and populated before insertion into this collection.");
|
||||
}
|
||||
|
||||
$className = $entityClass ?: $this->entityClass;
|
||||
|
||||
foreach($datasets as $dataset) {
|
||||
$this->append( $this->arrayToEntity($dataset, $entityClass) );
|
||||
$this->append( (new $className() )->fromArray($dataset) );
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function arrayToEntity(array $dataset, ? string /*stringable*/ $entityClass = null) : object
|
||||
public function mergeWith( /*array|EntityCollection*/ $datasets ) : self
|
||||
{
|
||||
if ( ! ($this->entityClass || $entityClass) ) {
|
||||
throw new \Exception("An entity class name must be provided to be instanciated and populated before insertion into this collection.");
|
||||
}
|
||||
|
||||
$className = $this->entityClass;
|
||||
|
||||
return ( new $className() )->fromArray($dataset);
|
||||
}
|
||||
|
||||
public function append($value) : void
|
||||
{
|
||||
if ( is_array($value) ) {
|
||||
$this->append( $this->arrayToEntity($value) );
|
||||
}
|
||||
else {
|
||||
parent::append($value);
|
||||
}
|
||||
}
|
||||
|
||||
public function mergeWith(... $datasets) : self
|
||||
{
|
||||
$list = [];
|
||||
|
||||
foreach($datasets as $dataset) {
|
||||
if ( is_object($dataset) ) {
|
||||
$list = array_merge($dataset->getArrayCopy(), $list);
|
||||
}
|
||||
else {
|
||||
$list = array_merge($dataset, $list);
|
||||
}
|
||||
if ( is_object($datasets) ) {
|
||||
$datasets = $datasets->getArrayCopy();
|
||||
}
|
||||
|
||||
$this->exchangeArray( array_merge( $this->getArrayCopy(), $list ) );
|
||||
$this->exchangeArray( array_merge( $this->getArrayCopy(), $datasets ) );
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -306,16 +271,4 @@ class EntityCollection extends \ArrayObject {
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function rsort(callable $callback, $function = "uasort") : self
|
||||
{
|
||||
$this->sort(...func_get_args());
|
||||
|
||||
return $this->reverse();
|
||||
}
|
||||
|
||||
public function reverse() : self
|
||||
{
|
||||
return $this->replaceWith(array_reverse($this->getArrayCopy()));;
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ use Ulmus\Repository,
|
||||
Ulmus\Common\EntityField;
|
||||
|
||||
use Ulmus\Annotation\Classes\{ Method, Table, Collation, };
|
||||
use Ulmus\Annotation\Property\{ Field, Filter, Relation, OrderBy, Where, Join, Virtual };
|
||||
use Ulmus\Annotation\Property\{ Field, Relation, OrderBy, Where, Join, Virtual };
|
||||
use Ulmus\Annotation\Property\Field\{ Id, ForeignKey, CreatedAt, UpdatedAt, };
|
||||
|
||||
trait EntityTrait {
|
||||
@ -63,15 +63,14 @@ trait EntityTrait {
|
||||
|
||||
$order = $entityResolver->searchFieldAnnotationList($name, new OrderBy() );
|
||||
$where = $entityResolver->searchFieldAnnotationList($name, new Where() );
|
||||
$filters = $entityResolver->searchFieldAnnotationList($name, new Filter() );
|
||||
|
||||
|
||||
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->where($condition->field, is_callable($condition->value) ? $f = call_user_func_array($condition->value, [ $this ]) : $condition->value, $condition->operator);
|
||||
}
|
||||
|
||||
foreach($order as $item) {
|
||||
@ -80,14 +79,6 @@ trait EntityTrait {
|
||||
|
||||
$field = $relation->key;
|
||||
}
|
||||
|
||||
$applyFilter = function($repository) use ($filters, $name) {
|
||||
foreach($filters as $filter) {
|
||||
$repository = call_user_func_array([ $this, $filter->method ], [ $repository, $name ]);
|
||||
}
|
||||
|
||||
return $repository;
|
||||
};
|
||||
|
||||
switch( $relationType ) {
|
||||
case 'onetoone':
|
||||
@ -109,10 +100,9 @@ trait EntityTrait {
|
||||
if ($relation->foreignKey) {
|
||||
$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);
|
||||
|
||||
return $this->$name = call_user_func([$applyFilter($repository), $relation->function]);
|
||||
return $this->$name = call_user_func([$repository, $relation->function]);
|
||||
|
||||
case 'manytomany':
|
||||
if ( false === $relation->bridge ?? false ) {
|
||||
@ -148,7 +138,7 @@ trait EntityTrait {
|
||||
|
||||
$this->eventExecute(Event\EntityRelationLoadInterface::class, $name, $repository);
|
||||
|
||||
$this->$name = call_user_func([ $applyFilter($repository), $relationRelation->function ]);
|
||||
$this->$name = call_user_func([ $repository, $relationRelation->function ]);
|
||||
|
||||
if ($relation->bridgeField ?? false) {
|
||||
$repository = $relationRelation->entity::repository();
|
||||
|
@ -64,11 +64,11 @@ class QueryBuilder
|
||||
{
|
||||
if ( null === $this->getFragment(Query\Insert::class) ) {
|
||||
if ( $schema ) {
|
||||
$table = "$schema.$table";
|
||||
$table = "\"$schema\".$table";
|
||||
}
|
||||
|
||||
|
||||
if ( $database ) {
|
||||
$table = "$database.$table";
|
||||
$table = "\"$database\".$table";
|
||||
}
|
||||
|
||||
$insert = new Query\Insert();
|
||||
@ -327,11 +327,11 @@ class QueryBuilder
|
||||
{
|
||||
if ( null === $this->getFragment(Query\Create::class) ) {
|
||||
if ( $schema ) {
|
||||
$table = "$schema.$table";
|
||||
$table = "\"$schema\".$table";
|
||||
}
|
||||
|
||||
|
||||
if ( $database ) {
|
||||
$table = "$database.$table";
|
||||
$table = "\"$database\".$table";
|
||||
}
|
||||
|
||||
$create = new Query\Create();
|
||||
|
@ -176,64 +176,6 @@ class Repository
|
||||
}
|
||||
}
|
||||
|
||||
public function loadCollectionRelation(EntityCollection $collection, /*array|string*/ $fields) : void
|
||||
{
|
||||
foreach((array) $fields as $name) {
|
||||
if ( null !== ( $relation = $this->entityResolver->searchFieldAnnotation($name, new Annotation\Property\Relation() ) ) ) {
|
||||
$relationType = strtolower(str_replace(['-', '_', ' '], '', $relation->type));
|
||||
|
||||
$order = $this->entityResolver->searchFieldAnnotationList($name, new Annotation\Property\OrderBy() );
|
||||
$where = $this->entityResolver->searchFieldAnnotationList($name, new Annotation\Property\Where() );
|
||||
|
||||
$baseEntity = $relation->entity ?? $relation->bridge ?? $this->entityResolver->properties[$name]['type'];
|
||||
$baseEntityResolver = $baseEntity::resolveEntity();
|
||||
|
||||
$property = ( $baseEntityResolver->field($relation->foreignKey, 01, false) ?: $baseEntityResolver->field($relation->foreignKey, 02) )['name'];
|
||||
$entityProperty = ( $this->entityResolver->field($relation->key, 01, false) ?: $this->entityResolver->field($relation->key, 02) )['name'];
|
||||
|
||||
$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);
|
||||
}
|
||||
|
||||
foreach($order as $item) {
|
||||
$repository->orderBy($item->field, $item->order);
|
||||
}
|
||||
|
||||
$field = $relation->key;
|
||||
|
||||
$values = [];
|
||||
|
||||
$key = is_object($relation->foreignKey) ? $relation->foreignKey : $baseEntity::field($relation->foreignKey);
|
||||
|
||||
foreach($collection as $item) {
|
||||
$values[] = is_callable($field) ? $field($item) : $item->$entityProperty;
|
||||
}
|
||||
|
||||
$repository->where($key, $values);
|
||||
|
||||
switch( $relationType ) {
|
||||
case 'onetoone':
|
||||
$results = call_user_func([ $repository, "loadOne" ]);
|
||||
$item->$name = $results ?: new $baseEntity();
|
||||
|
||||
break;
|
||||
|
||||
case 'onetomany':
|
||||
$results = call_user_func([ $repository, $relation->function ]);
|
||||
|
||||
foreach($collection as $item) {
|
||||
$item->$name = $baseEntity::entityCollection();
|
||||
$item->$name->mergeWith( $results->filtersCollection(fn($e) => $e->$property === $item->$entityProperty ) );
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function truncate(? string $table = null, ? string $alias = null, ? string $schema = null) : self
|
||||
{
|
||||
$schema = $schema ?: $this->entityResolver->schemaName();
|
||||
|
@ -25,9 +25,9 @@ trait ConditionTrait
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function where($field, $value, string $operator = Query\Where::OPERATOR_EQUAL, $condition = Query\Where::CONDITION_AND) : self
|
||||
public function where($field, $value, string $operator = Query\Where::OPERATOR_EQUAL) : self
|
||||
{
|
||||
$this->queryBuilder->where($field, $value, $operator, $condition);
|
||||
$this->queryBuilder->where($field, $value, $operator, Query\Where::CONDITION_AND);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@ -67,9 +67,9 @@ trait ConditionTrait
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function having($field, $value, string $operator = Query\Where::OPERATOR_EQUAL, $condition = Query\Where::CONDITION_AND) : self
|
||||
public function having($field, $value, string $operator = Query\Where::OPERATOR_EQUAL) : self
|
||||
{
|
||||
$this->queryBuilder->having($field, $value, $operator, $condition);
|
||||
$this->queryBuilder->having($field, $value, $operator, Query\Where::CONDITION_AND);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user