feat: add flatten() method

This commit is contained in:
Victor Westerlund 2023-11-02 10:18:27 +01:00
parent 8ff61b7275
commit 1df8076907

View file

@ -14,6 +14,7 @@
private string $table; private string $table;
private ?array $model = null; private ?array $model = null;
private bool $flatten = false;
private ?string $order_by = null; private ?string $order_by = null;
private ?string $filter_sql = null; private ?string $filter_sql = null;
private array $filter_values = []; private array $filter_values = [];
@ -115,6 +116,12 @@
return $this; return $this;
} }
// Flatten returned array to first entity if set
public function flatten(bool $flag = true): self {
$this->flatten = $flag;
return $this;
}
// Return SQL SORT BY string from assoc array of columns and direction // Return SQL SORT BY string from assoc array of columns and direction
public function order(array $order_by): self { public function order(array $order_by): self {
// Create CSV from columns // Create CSV from columns
@ -159,8 +166,8 @@
// Return array of matched rows // Return array of matched rows
$exec = $this->exec($sql, $this->filter_values); $exec = $this->exec($sql, $this->filter_values);
// Flatten array if LIMIT is 1 // Return array if exec was successful. Return as flattened array if flag is set
return empty($exec) || $this->limit !== 1 ? $exec : $exec[0]; return empty($exec) || !$this->flatten ? $exec : $exec[0];
} }
// Create Prepared Statement for UPDATE using PRIMARY KEY as anchor // Create Prepared Statement for UPDATE using PRIMARY KEY as anchor