|
|
@@ -36,6 +36,12 @@ abstract class ModelBase
|
|
|
return self::$config[3];
|
|
|
}
|
|
|
|
|
|
+ public function getTableName()
|
|
|
+ {
|
|
|
+ $className = new \ReflectionClass($this);
|
|
|
+ return $this->getDbPrefix().strtolower($className->getShortName());
|
|
|
+ }
|
|
|
+
|
|
|
public static function setup()
|
|
|
{
|
|
|
$tables = array("Admin", "User", "Address", "Cart", "Category", "Product", "CartProduct", "Meta", "Cms", "Config");
|
|
|
@@ -88,8 +94,6 @@ abstract class ModelBase
|
|
|
|
|
|
public function save()
|
|
|
{
|
|
|
- $className = new \ReflectionClass($this);
|
|
|
- $className = strtolower($className->getShortName());
|
|
|
//TODO send pre-hook
|
|
|
if ($this->id === null)
|
|
|
{
|
|
|
@@ -100,7 +104,7 @@ abstract class ModelBase
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- $query = "INSERT INTO `{$this->getDbPrefix()}{$className}` (`" .implode("`,`", array_keys($this->changed)) . "`) VALUES (" . implode(",", $this->changed) . ")";
|
|
|
+ $query = "INSERT INTO `{$this->getTableName()}` (`" .implode("`,`", array_keys($this->changed)) . "`) VALUES (" . implode(",", $this->changed) . ")";
|
|
|
$statement = self::$dbo->prepare($query);
|
|
|
$result = $statement->execute();
|
|
|
if (!$result)
|
|
|
@@ -117,6 +121,34 @@ abstract class ModelBase
|
|
|
//TODO send hook
|
|
|
}
|
|
|
|
|
|
+ public function selects($criteria = null)
|
|
|
+ {
|
|
|
+ $query = "SELECT * FROM {$this->getTableName()}";
|
|
|
+
|
|
|
+ if (!empty($criteria))
|
|
|
+ {
|
|
|
+ $subQuery = array();
|
|
|
+ foreach ($criteria as $i => $j)
|
|
|
+ {
|
|
|
+ if ($j == null)
|
|
|
+ $subQuery[] = "`{$i}` IS NULL";
|
|
|
+ else
|
|
|
+ $subQuery[] = "`{$i}`=".self::$dbo->quote($j);
|
|
|
+ }
|
|
|
+ $query .= " WHERE ".implode(" AND ", $subQuery);
|
|
|
+ }
|
|
|
+ $result = self::$dbo->query($query, \PDO::FETCH_ASSOC);
|
|
|
+ $resultObj = array();
|
|
|
+ $className = get_class($this);
|
|
|
+ foreach ($result as $i)
|
|
|
+ {
|
|
|
+ $iObj = new $className($this);
|
|
|
+ $iObj->populate($i);
|
|
|
+ $resultObj[] = $iObj;
|
|
|
+ }
|
|
|
+ return $resultObj;
|
|
|
+ }
|
|
|
+
|
|
|
//TODO
|
|
|
public function selectById($id)
|
|
|
{
|
|
|
@@ -125,6 +157,8 @@ abstract class ModelBase
|
|
|
private function populate($data)
|
|
|
{
|
|
|
$this->id = FALSE;
|
|
|
+ foreach ($data as $i => $j)
|
|
|
+ $this->fieldsValues[$i] = $j;
|
|
|
}
|
|
|
}
|
|
|
|