- More work done on Update, Insert and Delete clause
- Added TOP property to select and delete to fit MSSQL limit's term. - Added a new interface allowing object's manipulation on save() and load() queries
This commit is contained in:
1 parent
3dc82b1f0d
commit
2f43fb4559
13 files changed
+169
-58
No files matched your search
+48
-19
@@ -48,9 +48,11 @@ class Repository
|
||||
|
||||
public function count() : int
|
||||
{
|
||||
$this->select("count(*) as totalItem");
|
||||
$this->select("count(*) as totalItem")->selectSqlQuery();
|
||||
|
||||
$this->finalizeQuery();
|
||||
|
||||
foreach(Ulmus::iterateQueryBuilder($this->selectSqlQuery()->queryBuilder) as $entityData) {
|
||||
foreach(Ulmus::iterateQueryBuilder($this->queryBuilder) as $entityData) {
|
||||
return $entityData['totalItem'];
|
||||
}
|
||||
|
||||
@@ -58,7 +60,7 @@ class Repository
|
||||
}
|
||||
|
||||
public function deleteOne()
|
||||
{
|
||||
{
|
||||
return $this->limit(1)->deleteSqlQuery()->runQuery();
|
||||
}
|
||||
|
||||
@@ -67,14 +69,19 @@ class Repository
|
||||
return $this->deleteSqlQuery()->runQuery();
|
||||
}
|
||||
|
||||
public function deleteFromPk($value, $primaryKey = "id")
|
||||
public function deleteFromPk($value) : bool
|
||||
{
|
||||
return $this->where($primaryKey, $value)->deleteOne();
|
||||
if ( $value !== 0 && empty($value) ) {
|
||||
throw new Exception\EntityPrimaryKeyUnknown("A primary key value has to be defined to delete an item.");
|
||||
}
|
||||
|
||||
return (bool) $this->wherePrimaryKey($value)->deleteOne()->rowCount();
|
||||
}
|
||||
|
||||
public function save(object $entity) : bool
|
||||
{
|
||||
$dataset = $entity->toArray();
|
||||
|
||||
$primaryKeyDefinition = Ulmus::resolveEntity($this->entityClass)->getPrimaryKeyField();
|
||||
|
||||
if ( ! $entity->isLoaded() ) {
|
||||
@@ -85,18 +92,27 @@ class Repository
|
||||
$pkField = key($primaryKeyDefinition);
|
||||
$entity->$pkField = $statement->lastInsertId;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
if ($primaryKeyDefinition === null) {
|
||||
if ( $primaryKeyDefinition === null ) {
|
||||
throw new \Exception(sprintf("No primary key found for entity %s", $this->entityClass));
|
||||
}
|
||||
|
||||
$pkField = key($primaryKeyDefinition);
|
||||
$pkFieldName = $primaryKeyDefinition[$pkField]->name ?? $pkField;
|
||||
$this->where($pkFieldName, $dataset[$pkFieldName]);
|
||||
|
||||
# DATASET MUST BE DIFF OF MODIF ONLY !
|
||||
$update = $this->updateSqlQuery($dataset)->runQuery();
|
||||
|
||||
$diff = array_diff_assoc($dataset, $entity->entityGetDataset(true));
|
||||
|
||||
var_dump( "<pre>", $diff, $dataset, $entity->entityGetDataset(true) );
|
||||
|
||||
if ( [] !== $diff ) {
|
||||
$update = $this->updateSqlQuery($diff)->runQuery();
|
||||
|
||||
return (bool) $update->rowCount();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -156,7 +172,7 @@ class Repository
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function from(string $table, string $alias, ? string $schema) : self
|
||||
public function from(string $table, ? string $alias, ? string $schema) : self
|
||||
{
|
||||
$this->queryBuilder->from($table, $alias, null, $schema);
|
||||
|
||||
@@ -296,7 +312,7 @@ class Repository
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function orderBy(string $field, ? string $direction = null) : self
|
||||
public function orderBy($field, ? string $direction = null) : self
|
||||
{
|
||||
$this->queryBuilder->orderBy($field, $direction);
|
||||
|
||||
@@ -332,15 +348,24 @@ class Repository
|
||||
if ( null === $primaryKeyField = Ulmus::resolveEntity($this->entityClass)->getPrimaryKeyField() ) {
|
||||
throw new Exception\EntityPrimaryKeyUnknown("Entity has no field containing attributes 'primary_key'");
|
||||
}
|
||||
|
||||
$pkField = key($primaryKeyField);
|
||||
|
||||
return $this->where($primaryKeyField[$pkField]->name ?? $pkField, $value);
|
||||
}
|
||||
|
||||
protected function collectionFromQuery() : EntityCollection
|
||||
{
|
||||
|
||||
$class = $this->entityClass;
|
||||
|
||||
$entityCollection = new EntityCollection();
|
||||
|
||||
$this->selectSqlQuery();
|
||||
|
||||
$this->finalizeQuery();
|
||||
|
||||
foreach(Ulmus::iterateQueryBuilder($this->selectSqlQuery()->queryBuilder) as $entityData) {
|
||||
foreach(Ulmus::iterateQueryBuilder($this->queryBuilder) as $entityData) {
|
||||
$entityCollection->append( ( new $class() )->entityFillFromDataset($entityData) );
|
||||
}
|
||||
|
||||
@@ -349,12 +374,14 @@ class Repository
|
||||
|
||||
public function runQuery() : \PDOStatement
|
||||
{
|
||||
$this->finalizeQuery();
|
||||
|
||||
return Ulmus::runQuery($this->queryBuilder);
|
||||
}
|
||||
|
||||
protected function insertSqlQuery(array $dataset) : self
|
||||
{
|
||||
if ( ! $this->queryBuilder->has(Query\Insert::class) ) {
|
||||
if ( null === $this->queryBuilder->getFragment(Query\Insert::class) ) {
|
||||
$this->insert(array_keys($dataset), $this->entityResolver->tableName(), $this->alias, $this->entityResolver->schemaName());
|
||||
}
|
||||
|
||||
@@ -365,7 +392,7 @@ class Repository
|
||||
|
||||
protected function updateSqlQuery(array $dataset) : self
|
||||
{
|
||||
if ( ! $this->queryBuilder->has(Query\Update::class) ) {
|
||||
if ( null === $this->queryBuilder->getFragment(Query\Update::class) ) {
|
||||
$this->update($dataset, $this->entityResolver->tableName(), $this->alias, $this->entityResolver->schemaName());
|
||||
}
|
||||
|
||||
@@ -376,11 +403,11 @@ class Repository
|
||||
|
||||
protected function selectSqlQuery() : self
|
||||
{
|
||||
if ( ! $this->queryBuilder->has(Query\Select::class) ) {
|
||||
if ( null === $this->queryBuilder->getFragment(Query\Select::class) ) {
|
||||
$this->select("{$this->alias}.*");
|
||||
}
|
||||
|
||||
if ( ! $this->queryBuilder->has(Query\From::class) ) {
|
||||
if ( null === $this->queryBuilder->getFragment(Query\From::class) ) {
|
||||
$this->from($this->entityResolver->tableName(), $this->alias, $this->entityResolver->schemaName());
|
||||
}
|
||||
|
||||
@@ -389,12 +416,12 @@ class Repository
|
||||
|
||||
protected function deleteSqlQuery() : self
|
||||
{
|
||||
if ( ! $this->queryBuilder->has(Query\Delete::class) ) {
|
||||
if ( null === $this->queryBuilder->getFragment(Query\Delete::class) ) {
|
||||
$this->delete();
|
||||
}
|
||||
|
||||
if ( ! $this->queryBuilder->has(Query\From::class) ) {
|
||||
$this->from($this->entityResolver->tableName(), $this->alias, $this->entityResolver->schemaName());
|
||||
if ( null === $this->queryBuilder->getFragment(Query\From::class) ) {
|
||||
$this->from($this->entityResolver->tableName(), null, $this->entityResolver->schemaName());
|
||||
}
|
||||
|
||||
return $this;
|
||||
@@ -414,4 +441,6 @@ class Repository
|
||||
{
|
||||
return "REFLECT TABLE";
|
||||
}
|
||||
|
||||
protected function finalizeQuery() : void {}
|
||||
}
|
||||
Reference in new issue
Block a user