<?php require_once __DIR__ . '/../vendor/autoload.php'; use Utils\Connect; use Hydrate\Hydrate; use Utils\Parsing; $defaults = [PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8", PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC]; $database = (require_once __DIR__ . '/../config/database.php'); Connect::set(['dsn' => $database['dsn'], 'user' => $database['user'], 'password' => $database['password']], $defaults); /** * @hydrate data */ $parsing = new Parsing(__DIR__ . '/../src/dump/busy_day.in'); $sql = 'SELECT COUNT(*) FROM migrations'; $res = Connect::$pdo->query($sql); if ($res->fetchColumn() == 0) { $hydrate = new Hydrate(); $c = $parsing->get(); $hydrate->setProduct($c->allProduct()); $hydrate->setWarehouse($c->allWarehouse()); foreach ($c->allWarehouse() as $warehouse) { $warehouseId = $warehouse->id + 1; $hydrate->relWarehouseProd($warehouseId, $warehouse->allProduct()); } Connect::$pdo->query(sprintf("INSERT INTO `migrations` SET migration='%s', batch=%d", 'product_warehouse', 1)); $hydrate->setOrder($c->allOrder()); foreach ($c->allOrder() as $order) { $orderId = $order->id + 1; $hydrate->relOrderProd($orderId, $order->allProduct()); } Connect::$pdo->query(sprintf("INSERT INTO `migrations` SET migration='%s', batch=%d", 'order_product', 1));
<?php require_once __DIR__ . '/../src/Utils/Connect.php'; $database = (require_once __DIR__ . '/../config/database.php'); use Utils\Connect; Connect::set(['dsn' => $database['dsn'], 'user' => $database['user'], 'password' => $database['password']]); if (is_null(Connect::$pdo)) { throw new RuntimeException(sprintf('database connect migration fail')); } $db = Connect::$pdo; /** * @create migrations */ $count = $db->exec("\n CREATE TABLE migrations (\n `batch` INT UNSIGNED NOT NULL,\n `migration` VARCHAR(100)\n ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;\n "); /** * @create table warehouse */ $count = $db->exec("\n CREATE TABLE warehouses (\n `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,\n `row` SMALLINT ,\n `column` SMALLINT ,\n PRIMARY KEY (id)\n ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;\n "); /** * @create table products */ $count = $db->exec("\n CREATE TABLE products (\n `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,\n `weight` SMALLINT,\n PRIMARY KEY (id)\n ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;\n "); /** * @create table product_warehouse (abc order) */ $count = $db->exec("\n CREATE TABLE product_warehouse (\n warehouse_id INT UNSIGNED,\n product_id INT UNSIGNED,\n quantity INT NOT NULL DEFAULT 0,\n CONSTRAINT product_warehouse_warehouses_warehouse_id_foreign FOREIGN KEY(warehouse_id) REFERENCES warehouses(id) ON DELETE CASCADE,\n CONSTRAINT product_warehouse_products_product_id_foreign FOREIGN KEY(product_id) REFERENCES products(id) ON DELETE CASCADE,\n CONSTRAINT un_warehouse_id_product_id UNIQUE KEY (warehouse_id, product_id )\n ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;\n "); $count = $db->exec("\n CREATE TABLE orders (\n `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,\n `row` SMALLINT,\n `column` SMALLINT,\n `items` SMALLINT,\n PRIMARY KEY (id)\n ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;\n "); $count = $db->exec("\n CREATE TABLE order_product (\n order_id INT UNSIGNED,\n product_id INT UNSIGNED,\n quantity INT NOT NULL DEFAULT 0,\n CONSTRAINT order_product_orders_order_id_foreign FOREIGN KEY(order_id) REFERENCES orders(id) ON DELETE CASCADE,\n CONSTRAINT order_product_products_product_id_foreign FOREIGN KEY(product_id) REFERENCES products(id) ON DELETE CASCADE,\n CONSTRAINT un_order_id_product_id UNIQUE KEY (order_id, product_id )\n ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;\n ");