commit
4b3adfb77c
2 changed files with 213 additions and 0 deletions
121
database/DataBase.php
Normal file
121
database/DataBase.php
Normal file
|
@ -0,0 +1,121 @@
|
|||
<?php
|
||||
|
||||
namespace Database;
|
||||
|
||||
use Exception;
|
||||
use PDO;
|
||||
|
||||
class Database
|
||||
{
|
||||
|
||||
private $connection;
|
||||
private $options = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"];
|
||||
|
||||
private $dbHost = DB_HOST;
|
||||
private $dbName = DB_NAME;
|
||||
private $dbUsername = DB_USERNAME;
|
||||
private $dbPassword = DB_PASSWORD;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
try {
|
||||
$this->connection = new PDO('mysql:host=' . $this->dbHost . ";dbname=" . $this->dbName, $this->dbUsername, $this->dbPassword, $this->options);
|
||||
|
||||
} catch (Exception $e) {
|
||||
echo 'error ' . $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// select('select * from user');
|
||||
// select('select * from user WHERE id = ?', [2]);
|
||||
public function select($sql, $values = null)
|
||||
{
|
||||
try {
|
||||
|
||||
$statement = $this->connection->prepare($sql);
|
||||
if ($values == null) {
|
||||
$statement->execute();
|
||||
} else {
|
||||
$statement->execute($values);
|
||||
}
|
||||
$result = $statement;
|
||||
return $result;
|
||||
|
||||
} catch (Exception $e) {
|
||||
echo 'error ' . $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// insert('users', ['email', 'age'], ['ali@yahoo.com', 20]);
|
||||
public function insert($tableName, $fields, $values)
|
||||
{
|
||||
try {
|
||||
$statement = $this->connection->prepare("INSERT INTO " . $tableName . "(" . implode(', ', $fields) . ", created_at) VALUES ( :" . implode(', :', $fields) . ", now() );");
|
||||
$statement->execute(array_combine($fields, $values));
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
echo 'error ' . $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// update('users', 2, ['email', 'age'], ['ali@yahoo.com', 20]);
|
||||
// 0 => 'ali@yahoo.com',
|
||||
public function update($tableName, $id, $fields, $values)
|
||||
{
|
||||
$sql = "UPDATE " . $tableName . " SET ";
|
||||
foreach (array_combine($fields, $values) as $field => $value) {
|
||||
if ($value) {
|
||||
$sql .= $field . ' = ?, ';
|
||||
} else {
|
||||
$sql .= $field . ' = NULL, ';
|
||||
}
|
||||
}
|
||||
$sql .= " updated_at = now()";
|
||||
$sql .= " WHERE id = ?";
|
||||
|
||||
try {
|
||||
|
||||
$statement = $this->connection->prepare($sql);
|
||||
$statement->execute(array_merge(array_filter(array_values($values)), [$id]));
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
echo 'error ' . $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// delete('users', 2)
|
||||
public function delete($tableName, $id)
|
||||
{
|
||||
$sql = "DELETE FROM " . $tableName . " WHERE id = ?";
|
||||
try {
|
||||
$statement = $this->connection->prepare($sql);
|
||||
$statement->execute([$id]);
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
echo 'error ' . $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function createTable($query)
|
||||
{
|
||||
try {
|
||||
$this->connection->exec($query);
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
echo 'error ' . $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
92
database/createDB.php
Normal file
92
database/createDB.php
Normal file
|
@ -0,0 +1,92 @@
|
|||
<?php
|
||||
|
||||
use Database\Database;
|
||||
|
||||
class CreateDB extends Database
|
||||
{
|
||||
|
||||
private $queries = [
|
||||
"CREATE TABLE `categories` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT ,
|
||||
`name` varchar(200) COLLATE utf8_persian_ci NOT NULL,
|
||||
`created_at` datetime NOT NULL,
|
||||
`updated_at` datetime DEFAULT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci;",
|
||||
|
||||
"CREATE TABLE `users` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`username` varchar(100) COLLATE utf8_persian_ci NOT NULL,
|
||||
`email` varchar(100) COLLATE utf8_persian_ci NOT NULL,
|
||||
`password` varchar(100) COLLATE utf8_persian_ci NOT NULL,
|
||||
`permission` enum('user','admin') COLLATE utf8_persian_ci NOT NULL DEFAULT 'user',
|
||||
`created_at` datetime NOT NULL,
|
||||
`updated_at` datetime DEFAULT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `email` (`email`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci;",
|
||||
|
||||
"CREATE TABLE `articles` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`title` varchar(200) COLLATE utf8_persian_ci NOT NULL,
|
||||
`summary` text COLLATE utf8_persian_ci NOT NULL,
|
||||
`body` text COLLATE utf8_persian_ci NOT NULL,
|
||||
`view` int(11) NOT NULL DEFAULT '0',
|
||||
`user_id` int(11) NOT NULL,
|
||||
`cat_id` int(11) NOT NULL,
|
||||
`image` varchar(200) COLLATE utf8_persian_ci NOT NULL,
|
||||
`status` enum('disable','enable') COLLATE utf8_persian_ci NOT NULL DEFAULT 'disable',
|
||||
`created_at` datetime NOT NULL,
|
||||
`updated_at` datetime DEFAULT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
FOREIGN KEY (`cat_id`) REFERENCES `categories` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci;",
|
||||
|
||||
"CREATE TABLE `comments` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`user_id` int(11) NOT NULL,
|
||||
`comment` text COLLATE utf8_persian_ci NOT NULL,
|
||||
`article_id` int(11) NOT NULL,
|
||||
`status` enum('unseen','seen','approved') COLLATE utf8_persian_ci NOT NULL DEFAULT 'unseen',
|
||||
`created_at` datetime NOT NULL,
|
||||
`updated_at` datetime DEFAULT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
FOREIGN KEY (`article_id`) REFERENCES `articles` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci;",
|
||||
|
||||
"CREATE TABLE `websetting` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`title` text COLLATE utf8_persian_ci DEFAULT NULL,
|
||||
`description` text COLLATE utf8_persian_ci DEFAULT NULL,
|
||||
`keywords` text COLLATE utf8_persian_ci DEFAULT NULL,
|
||||
`logo` text COLLATE utf8_persian_ci DEFAULT NULL,
|
||||
`icon` text COLLATE utf8_persian_ci DEFAULT NULL,
|
||||
`created_at` datetime NOT NULL,
|
||||
`updated_at` datetime DEFAULT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci;
|
||||
",
|
||||
|
||||
"CREATE TABLE `menus` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`name` varchar(100) COLLATE utf8_persian_ci NOT NULL,
|
||||
`url` varchar(300) COLLATE utf8_persian_ci NOT NULL,
|
||||
`parent_id` int(11) DEFAULT NULL,
|
||||
`created_at` datetime NOT NULL,
|
||||
`updated_at` datetime DEFAULT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
FOREIGN KEY (`parent_id`) REFERENCES `menus` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci;
|
||||
",
|
||||
];
|
||||
|
||||
public function run()
|
||||
{
|
||||
foreach ($this->queries as $query) {
|
||||
$this->createTable($query);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue