- Still working on Update and Insert statements
This commit is contained in:
1 parent
828daffec8
commit
3dc82b1f0d
11 files changed
+243
-62
No files matched your search
+45
-10
@@ -68,19 +68,35 @@ class Repository
|
||||
}
|
||||
|
||||
public function deleteFromPk($value, $primaryKey = "id")
|
||||
{
|
||||
{
|
||||
return $this->where($primaryKey, $value)->deleteOne();
|
||||
}
|
||||
|
||||
public function save(object $entity) : bool
|
||||
{
|
||||
{
|
||||
$dataset = $entity->toArray();
|
||||
|
||||
$primaryKeyDefinition = Ulmus::resolveEntity($this->entityClass)->getPrimaryKeyField();
|
||||
|
||||
if ( ! $entity->isLoaded() ) {
|
||||
$save = $this->insertSqlQuery($dataset)->runQuery();
|
||||
$statement = $this->insertSqlQuery($dataset)->runQuery();
|
||||
|
||||
if ( ( 0 !== $statement->lastInsertId ) &&
|
||||
( null !== $primaryKeyDefinition )) {
|
||||
$pkField = key($primaryKeyDefinition);
|
||||
$entity->$pkField = $statement->lastInsertId;
|
||||
}
|
||||
}
|
||||
else {
|
||||
$save = $this->updateSqlQuery($dataset)->runQuery();
|
||||
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();
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -119,6 +135,20 @@ class Repository
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function update(array $fieldlist, string $table, string $alias, ? string $schema) : self
|
||||
{
|
||||
$this->queryBuilder->update($table, $alias, $schema);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function set(array $dataset) : self
|
||||
{
|
||||
$this->queryBuilder->set($dataset);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function delete() : self
|
||||
{
|
||||
$this->queryBuilder->delete($this->alias);
|
||||
@@ -176,7 +206,7 @@ class Repository
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function notWhere(array $condition) : self
|
||||
public function notWhere($field, $value, string $operator = Query\Where::OPERATOR_NOT_EQUAL) : self
|
||||
{
|
||||
$this->queryBuilder->where($field, $value, $operator, Query\Where::CONDITION_AND, true);
|
||||
|
||||
@@ -269,18 +299,21 @@ class Repository
|
||||
public function orderBy(string $field, ? string $direction = null) : self
|
||||
{
|
||||
$this->queryBuilder->orderBy($field, $direction);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function limit(int $value) : self
|
||||
{
|
||||
$this->queryBuilder->limit($value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function offset(int $value) : self
|
||||
{
|
||||
$this->queryBuilder->offset($value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -314,7 +347,7 @@ class Repository
|
||||
return $entityCollection;
|
||||
}
|
||||
|
||||
public function runQuery()
|
||||
public function runQuery() : \PDOStatement
|
||||
{
|
||||
return Ulmus::runQuery($this->queryBuilder);
|
||||
}
|
||||
@@ -322,10 +355,10 @@ class Repository
|
||||
protected function insertSqlQuery(array $dataset) : self
|
||||
{
|
||||
if ( ! $this->queryBuilder->has(Query\Insert::class) ) {
|
||||
$this->insert(array_keys($dataset));
|
||||
$this->insert(array_keys($dataset), $this->entityResolver->tableName(), $this->alias, $this->entityResolver->schemaName());
|
||||
}
|
||||
|
||||
$this->values(count($dataset));
|
||||
$this->values($dataset);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -333,8 +366,10 @@ class Repository
|
||||
protected function updateSqlQuery(array $dataset) : self
|
||||
{
|
||||
if ( ! $this->queryBuilder->has(Query\Update::class) ) {
|
||||
# $this->insert();
|
||||
$this->update($dataset, $this->entityResolver->tableName(), $this->alias, $this->entityResolver->schemaName());
|
||||
}
|
||||
|
||||
$this->set($dataset);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user