- Added a new value binding methods

This commit is contained in:
Dave Mc Nicoll 2023-11-20 15:21:17 -05:00
parent 62792e8325
commit afe5144dc7
3 changed files with 37 additions and 23 deletions

View File

@ -82,27 +82,7 @@ class PdoObject extends PDO {
$this->beginTransaction();
}
foreach($parameters as $key => $value) {
switch(strtolower(gettype($value))) {
case "boolean":
$type = Pdo::PARAM_BOOL;
break;
case "integer":
$type = Pdo::PARAM_INT;
break;
case "null":
$type = Pdo::PARAM_NULL;
break;
case "string":
default:
$type = Pdo::PARAM_STR;
}
$statement->bindValue($key, (string) $value, $type);
}
$this->bindVariables($statement, $parameters);
$this->executionStatus = $statement->execute();
@ -131,4 +111,36 @@ class PdoObject extends PDO {
return null;
}
protected function bindVariables(PDOStatement $statement, array &$parameters) : void
{
if ($parameters) {
if (array_is_list($parameters)) {
$parameters = array_combine(range(1, count($parameters)), array_values($parameters));
}
else {
foreach ($parameters as $key => $value) {
switch (strtolower(gettype($value))) {
case "boolean":
$type = Pdo::PARAM_BOOL;
break;
case "integer":
$type = Pdo::PARAM_INT;
break;
case "null":
$type = Pdo::PARAM_NULL;
break;
case "string":
default:
$type = Pdo::PARAM_STR;
}
$statement->bindValue($key, $value, $type);
}
}
}
}
}

View File

@ -27,7 +27,7 @@ class Values extends Fragment {
# index starts at '1' for Pdo params
$this->rows[] = array_combine(range(1, count($row)), array_values($row));
return $this;
}

View File

@ -242,7 +242,7 @@ class Repository
$primaryKeyDefinition = Ulmus::resolveEntity($this->entityClass)->getPrimaryKeyField();
if ( $replace || ! $entity->isLoaded() ) {
if ( ! $entity->isLoaded() ) {
# $dataset = array_filter($dataset, fn($item, $field) => ! ($this->entityResolver->searchFieldAnnotation($field, new Field, false)->readonly ?? false), \ARRAY_FILTER_USE_BOTH);
$statement = $this->insertSqlQuery($fieldsAndValue ?? $dataset, $replace)->runInsertQuery();
@ -277,6 +277,8 @@ class Repository
return $update ? (bool) $update->rowCount : false;
}
}
return 0;
}
public function replace(object|array $entity, ? array $fieldsAndValue = null) : bool