- Added some new method to EntityCollection (replaceWith, mergeWith, randomize, sort)

- WIP on virtual fields
- Some minor bugfixes
This commit is contained in:
Dave Mc Nicoll committed 2020-11-27 12:09:15 -05:00
1 parent c8397484e6
commit 30a0a80cbe
9 files changed
+213 -47

No files matched your search

+36 -11
View File
@@ -29,6 +29,11 @@ class Repository
$this->adapter = $adapter ?? $this->entityResolver->databaseAdapter() ?? Ulmus::$defaultAdapter;
$this->queryBuilder = new QueryBuilder();
}
public function __clone()
{
#$this->queryBuilder = clone $this->queryBuilder;
}
public function loadOne() : ? object
{
@@ -137,9 +142,11 @@ class Repository
if ( ( 0 !== $statement->lastInsertId ) &&
( null !== $primaryKeyDefinition )) {
$pkField = key($primaryKeyDefinition);
$entity->$pkField = $statement->lastInsertId;
$dataset[$pkField] = $statement->lastInsertId;
}
$entity->entityFillFromDataset($dataset, true);
return true;
}
else {
@@ -153,6 +160,7 @@ class Repository
$this->where($pkFieldName, $dataset[$pkFieldName]);
$update = $this->updateSqlQuery($diff)->runQuery();
$entity->entityFillFromDataset($dataset);
return $update ? (bool) $update->rowCount() : false;
}
@@ -199,7 +207,7 @@ class Repository
}
}
public function select($fields) : self
public function select(/*array|Stringable*/ $fields) : self
{
$this->queryBuilder->select($fields);
@@ -313,6 +321,14 @@ class Repository
return $this;
}
# @UNTESTED
public function randomizeOrder() : self
{
$this->queryBuilder->orderBy(Common\Sql::function('RAND', Sql::identifier('CURDATE()+0')));
return $this;
}
public function orders(array $orderList) : self
{
foreach($orderList as $field => $direction) {
@@ -361,7 +377,7 @@ class Repository
return $this->where($primaryKeyField[$pkField]->name ?? $pkField, $value);
}
public function withJoin(/*string|array*/ $fields) : self
public function withJoin(/*stringable|array*/ $fields) : self
{
$resolvedEntity = Ulmus::resolveEntity($this->entityClass);
@@ -374,7 +390,7 @@ class Repository
$alias = $join->alias ?? $item;
$entity = $join->entity ?? $resolvedEntity->properties[$item]['type'];
foreach($entity::resolveEntity()->fieldList(Common\EntityResolver::KEY_COLUMN_NAME) as $key => $field) {
foreach($entity::resolveEntity()->fieldList(Common\EntityResolver::KEY_COLUMN_NAME, true) as $key => $field) {
$this->select("$alias.$key as {$alias}\${$field['name']}");
}
@@ -397,17 +413,19 @@ class Repository
$wheres = $searchRequest->wheres();
$groups = $searchRequest->groups();
$searchRequest->count = $searchRequest->skipCount ? 0 : $searchRequest->filter( clone $this )
$searchRequest->filter( $this )
->wheres($wheres, Query\Where::OPERATOR_EQUAL, Query\Where::CONDITION_AND)
->likes($likes, Query\Where::CONDITION_OR)
->groups($groups)
->count();
->groups($groups);
return $searchRequest->filter($this)
$searchRequest->count = $searchRequest->skipCount ? 0 : (clone $this)->count();
return $searchRequest
->filter($this)
->wheres($wheres, Query\Where::OPERATOR_EQUAL, Query\Where::CONDITION_AND)
->likes($likes, Query\Where::CONDITION_OR)
->orders($searchRequest->orders())
->groups($groups)
->orders($searchRequest->orders())
->offset($searchRequest->offset())
->limit($searchRequest->limit());
}
@@ -447,6 +465,13 @@ class Repository
return Ulmus::runQuery($this->queryBuilder, $this->adapter);
}
public function resetQuery() : self
{
$this->queryBuilder->reset();
return $this;
}
protected function insertSqlQuery(array $dataset) : self
{
if ( null === $this->queryBuilder->getFragment(Query\Insert::class) ) {
@@ -505,9 +530,9 @@ class Repository
}
public function instanciateEntityCollection() : EntityCollection
public function instanciateEntityCollection(...$arguments) : EntityCollection
{
return $this->entityClass::entityCollection();
return $this->entityClass::entityCollection(...$arguments);
}
public function escapeTable(string $identifier) : string