Skip to content

Commit

Permalink
update model
Browse files Browse the repository at this point in the history
  • Loading branch information
brunobmorais committed Nov 13, 2024
1 parent 776ac68 commit 87230e5
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/Crud.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,23 @@ public function insertObject(object $object)
{
$args = [];
$params = [];

// Filtrar propriedades do objeto que não são null
foreach ($object as $chave => $valor) {
if ($valor != null) {
if ($valor !== null) { // Verifica explicitamente se o valor não é null
$args[] = $chave;
$params[] = $valor;
}
}
$args = implode(',', $args);

return $this->insert($args, $params);
// Se houver colunas a serem inseridas
if (!empty($args)) {
$columns = implode(',', $args);
return $this->insert($columns, $params);
}

// Retorna falso se todos os valores forem null
return false;
}

/**
Expand All @@ -99,32 +107,35 @@ public function insertObject(object $object)
*/
public function insertArray(array $params): bool
{
// Remove colunas com valores null
$params = array_filter($params, fn($value) => $value !== null);

if (!empty($params)) {
$query = "INSERT INTO {$this->getTableName()}";
$values = [];
$dataColumns = array_keys($params);

if (isset($dataColumns[0])) {
$query .= ' (`' . implode('`, `', $dataColumns) . '`) ';
}

$query .= ' VALUES (';

foreach ($dataColumns as $index => $column) {
foreach ($dataColumns as $column) {
$values[] = $params[$column];
$query .= '?, ';
}

// Remove a última vírgula e espaço
$query = rtrim($query, ', ');
$query .= ')';

$result = $this->executeSQL($query, $values);
if (empty($result)) {
return false;
}

return true;
} else {
return false;
return !empty($result);
}

return false;
}

/**
Expand Down Expand Up @@ -247,4 +258,4 @@ public function getLogSQL(): ?string
return $this->logSQL ?? "";
}

}
}

0 comments on commit 87230e5

Please sign in to comment.