Skip to content

Commit 2e36889

Browse files
committed
set alias for querying
Signed-off-by: Derek Smart <derek@grindaga.com>
1 parent 2adef5b commit 2e36889

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/Model.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,38 @@
33

44
abstract class Model extends PHPRed
55
{
6+
private $alias;
7+
68
public $model;
79
public $table;
810
public $primaryKey;
911

1012
public function __construct(\mysqli $mysqli)
1113
{
14+
$this->setAlias();
1215
parent::__construct($mysqli);
1316
}
1417

18+
private function setAlias()
19+
{
20+
$this->alias = $this->table . ' ' . $this->model;
21+
}
22+
1523
public function getAll() : array
1624
{
17-
$query = "SELECT * FROM $this->table;";
25+
$query = "SELECT $this->model.* FROM $this->alias;";
1826
return $this->query($query);
1927
}
2028

2129
public function getById(int $modelId) : array
2230
{
23-
$query = "SELECT * FROM $this->table WHERE $this->primaryKey = $modelId;";
31+
$query = "SELECT $this->model.* FROM $this->alias WHERE $this->model.$this->primaryKey = $modelId;";
2432
return $this->query($query);
2533
}
2634

2735
public function get() : array
2836
{
29-
$query = "SELECT * FROM $this->table ";
37+
$query = "SELECT $this->model.* FROM $this->alias";
3038
$query .= $this->conditions;
3139
$query .= $this->order;
3240
return $this->query($query);

src/PHPRed.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function __construct(\mysqli $mysqli)
1616

1717
protected function query(string $query) : array
1818
{
19+
echo PHP_EOL . $query . PHP_EOL;
1920
$results = $this->mysqli->query($query);
2021
if ($this->mysqli->errno) {
2122
throw new \Exception($this->mysqli->error . $query);
@@ -31,26 +32,26 @@ protected function query(string $query) : array
3132

3233
public function orderBy(array $args)
3334
{
34-
$order = 'ORDER BY ';
35+
$order = ' ORDER BY ';
3536
foreach ($args as $key => $value) {
3637
$key = $this->mysqli->escape_string($key);
3738
$value = $this->mysqli->escape_string($value);
38-
$order .= '`' . $key . '` ' . strtoupper($value) . ' ';
39+
$order .= $this->model . '.`' . $key . '` ' . strtoupper($value) . ', ';
3940
}
40-
$this->order = trim($order);
41+
$this->order = rtrim(substr($order, 0, -2));
4142
return $this;
4243
}
4344

4445

4546
public function where(array $args, string $method = 'AND')
4647
{
47-
$search = 'WHERE ';
48+
$search = ' WHERE ';
4849
foreach ($args as $key => $value) {
4950
$key = $this->mysqli->escape_string($key);
5051
$value = $this->mysqli->escape_string($value);
51-
$search .= '`' . $key . '` = "' . $value . '" ' . $method;
52+
$search .= $this->model . '.`' . $key . '` = "' . $value . '" ' . $method;
5253
}
53-
$this->conditions .= trim(substr($search, 0, -strlen($method)));
54+
$this->conditions .= rtrim(substr($search, 0, -strlen($method)));
5455
return $this;
5556
}
5657
}

0 commit comments

Comments
 (0)