|
|
@@ -44,7 +44,7 @@ abstract class ModelBase
|
|
|
|
|
|
public static function setup()
|
|
|
{
|
|
|
- $tables = array("Admin", "User", "Address", "Cart", "Category", "Product", "CartProduct", "Meta", "Cms", "Config");
|
|
|
+ $tables = array("Admin", "User", "Address", "Cart", "Category", "Product", "CartProduct", "Meta", "Cms", "Config", "Module");
|
|
|
|
|
|
self::init();
|
|
|
self::$dbo->beginTransaction();
|
|
|
@@ -88,7 +88,10 @@ abstract class ModelBase
|
|
|
if ($value instanceof \DateTime)
|
|
|
$value = $value->format("Y-m-d");
|
|
|
$this->fieldsValues[$key] = $value;
|
|
|
- $this->changed[$key] = self::$dbo->quote($value);
|
|
|
+ if (is_bool($value))
|
|
|
+ $this->changed[$key] = $value ? 1 : 0;
|
|
|
+ else
|
|
|
+ $this->changed[$key] = self::$dbo->quote($value);
|
|
|
return $value;
|
|
|
}
|
|
|
|
|
|
@@ -105,18 +108,28 @@ abstract class ModelBase
|
|
|
else
|
|
|
{
|
|
|
$query = "INSERT INTO `{$this->getTableName()}` (`" .implode("`,`", array_keys($this->changed)) . "`) VALUES (" . implode(",", $this->changed) . ")";
|
|
|
- $statement = self::$dbo->prepare($query);
|
|
|
- $result = $statement->execute();
|
|
|
+ $result = self::$dbo->exec($query);
|
|
|
if (!$result)
|
|
|
- throw new \Exception($statement->errorInfo()[2]);
|
|
|
+ throw new \Exception(self::$dbo->errorInfo()[2]);
|
|
|
$this->changed = array();
|
|
|
}
|
|
|
//TODO get last id
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
+ if (empty($this->changed))
|
|
|
+ return true;
|
|
|
if ($this->id === false)
|
|
|
throw new \Exception("Cannot update private row");
|
|
|
+ $query = "UPDATE {$this->getTableName()} SET ";
|
|
|
+ $newValues = array();
|
|
|
+ foreach ($this->changed as $i => $j)
|
|
|
+ $newValues[] = "`{$i}`=" . self::$dbo->quote($j);
|
|
|
+ $query .= implode(",",$newValues)." WHERE id={$this->id}";
|
|
|
+ $result = self::$dbo->exec($query);
|
|
|
+ if (!$result)
|
|
|
+ throw new \Exception(self::$dbo->errorInfo()[2]);
|
|
|
+ $this->changed = array();
|
|
|
}
|
|
|
//TODO send hook
|
|
|
}
|
|
|
@@ -138,6 +151,8 @@ abstract class ModelBase
|
|
|
$query .= " WHERE ".implode(" AND ", $subQuery);
|
|
|
}
|
|
|
$result = self::$dbo->query($query, \PDO::FETCH_ASSOC);
|
|
|
+ if ($result === false)
|
|
|
+ throw new \Exception(self::$dbo->errorInfo()[2]);
|
|
|
$resultObj = array();
|
|
|
$className = get_class($this);
|
|
|
foreach ($result as $i)
|
|
|
@@ -159,6 +174,8 @@ abstract class ModelBase
|
|
|
$this->id = FALSE;
|
|
|
foreach ($data as $i => $j)
|
|
|
$this->fieldsValues[$i] = $j;
|
|
|
+ if (isset($data["id"]))
|
|
|
+ $this->id = (int) $data["id"];
|
|
|
}
|
|
|
}
|
|
|
|