/** * Run the migrations * @return mixed */ public function up() { $query = "ALTER TABLE `" . App::getDbName() . "`.`" . Vessel::$tableName . "` " . "ADD COLUMN `" . Vessel::$columnTypeId . "` INT NULL, " . "ADD INDEX `" . Vessel::$indexTypesVessels . "` (`" . Vessel::$columnTypeId . "` ASC);"; $this->db->getConnection()->prepare($query)->execute(); $query = "ALTER TABLE `" . App::getDbName() . "`.`" . Vessel::$tableName . "` " . "ADD CONSTRAINT `" . Vessel::$foreignTypesVessels . "` " . "FOREIGN KEY (`" . Vessel::$columnTypeId . "`) " . "REFERENCES `" . App::getDbName() . "`.`" . Type::$tableName . "` (`" . Type::$columnPrimaryKey . "`) " . "ON DELETE RESTRICT ON UPDATE CASCADE;"; $this->db->getConnection()->prepare($query)->execute(); echo "Added type to vessels table.\n"; }
/** * Run the migrations * @return mixed */ public function up() { $query = "ALTER TABLE `" . App::getDbName() . "`.`" . Company::$tableName . "` " . "ADD COLUMN `" . Company::$columnUserId . "` INT NULL, " . "ADD UNIQUE INDEX `" . Company::$indexCompaniesUsers . "` (`" . Company::$columnUserId . "` ASC);"; $this->db->getConnection()->prepare($query)->execute(); $query = "ALTER TABLE `" . App::getDbName() . "`.`" . Company::$tableName . "` " . "ADD CONSTRAINT `" . Company::$foreignCompaniesUsers . "` " . "FOREIGN KEY (`" . Company::$columnUserId . "`) " . "REFERENCES `" . App::getDbName() . "`.`" . User::$tableName . "` (`" . User::$columnPrimaryKey . "`) " . "ON DELETE RESTRICT\tON UPDATE CASCADE;"; $this->db->getConnection()->prepare($query)->execute(); echo "Added user to company table.\n"; }
/** * @return mixed */ public function run() { foreach (range(1, 3) as $index) { $name = $this->faker->unique()->randomElement($this->popularCompanies); $query = "INSERT INTO `" . App::getDbName() . "`.`" . Company::$tableName . "` " . "(`" . Company::$columnName . "`) " . "VALUES ('{$name}')"; DbManager::getConnection()->query($query); } echo "Seed for '" . Company::$tableName . "' table complete.\n"; }
/** * @param $userId * @return mixed */ public function getById($userId) { $query = "SELECT * FROM `" . App::getDbName() . "`.`" . User::$tableName . "` " . "INNER JOIN `" . Company::$tableName . "` ON `" . Company::$tableName . "`.`" . Company::$columnUserId . "` = `" . User::$tableName . "`.`" . User::$columnPrimaryKey . "` " . "WHERE `" . User::$tableName . "`.`" . User::$columnPrimaryKey . "` = :user_id "; $query = DbManager::getConnection()->prepare($query); $query->bindParam(':user_id', $userId, PDO::PARAM_STR); if ($query->execute()) { return $query->fetch(PDO::FETCH_ASSOC); } return false; }
/** * @return mixed */ public function run() { foreach (range(0, sizeof($this->popularNames) - 1) as $index) { $imo = "IMO" . $this->faker->unique()->randomNumber(7); $name = $this->faker->unique()->randomElement($this->popularNames); $query = "INSERT INTO `" . App::getDbName() . "`.`" . Vessel::$tableName . "` " . "(`" . Vessel::$columnImo . "`, `" . Vessel::$columnName . "`) " . "VALUES ('{$imo}', '{$name}')"; $query = DbManager::getConnection()->query($query); } echo "Seed for '" . Vessel::$tableName . "' table complete.\n"; }
public function __construct() { try { $this->dbConnection = new PDO("mysql:host=" . App::getDbHost() . ";" . "dbname=" . App::getDbName() . ";" . "port=" . App::getDbPort(), App::getDbUsername(), App::getDbPassword()); $this->dbConnection->setAttribute(PDO::ATTR_ERRMODE, App::getPDOErrorMode()); // CHANGE THE ERROR MODE, THROW AN EXCEPTION WHEN AN ERROR IS FOUND $this->dbConnection->exec("SET NAMES 'utf8'"); } catch (PDOException $e) { throw new Exception("Could not connect to the database.\n" . $e->getMessage() . "\n\n"); } }
/** * @return mixed */ public function run() { foreach (range(0, 3) as $index) { $firstName = $this->faker->firstName(); $lastName = $this->faker->lastName(); $email = $this->faker->unique()->email(); $password = password_hash($this->faker->password(), PASSWORD_DEFAULT); $query = "INSERT INTO `" . App::getDbName() . "`.`" . User::$tableName . "` " . "(`" . User::$columnFirstName . "`, `" . User::$columnLastName . "`," . "`" . User::$columnEmail . "`, `" . User::$columnPassword . "`) " . "VALUES ('{$firstName}', '{$lastName}', '{$email}', '{$password}')"; $query = DbManager::getConnection()->query($query); } echo "Seed for '" . User::$tableName . "' table complete.\n"; }
/** * @param int $from * @param int $to * @return mixed */ public function getWithRelations($from = 1, $to = 10) { $offset = $from - 1; $rows = $to - $from + 1; $query = "SELECT * FROM `" . App::getDbName() . "`.`" . Company::$tableName . "` " . "LEFT JOIN `" . User::$tableName . "` ON `" . Company::$tableName . "`.`" . Company::$columnUserId . "` = `" . User::$tableName . "`.`" . User::$columnPrimaryKey . "` " . "LIMIT {$from}, {$to}"; $query = DbManager::getConnection()->prepare($query); $query->bindParam(1, $offset, PDO::PARAM_INT); $query->bindParam(2, $rows, PDO::PARAM_INT); if ($query->execute()) { return $query->fetchAll(PDO::FETCH_ASSOC); } return false; }
/** * @return mixed */ public function run() { $companyIds = $this->companyRepository->getAll([Company::$columnPrimaryKey]); $vesselIds = $this->vesselRepository->getAll([Vessel::$columnPrimaryKey]); foreach (range(0, 33) as $index) { $companyId = $this->faker->randomElement($companyIds)[Company::$columnPrimaryKey]; $vesselId = $this->faker->randomElement($vesselIds)[Vessel::$columnPrimaryKey]; $query = "UPDATE `" . App::getDbName() . "`.`" . Vessel::$tableName . "` " . "SET `" . Vessel::$columnCompanyId . "`= :company_id " . "WHERE `" . Vessel::$columnPrimaryKey . "` = :vessel_id"; $query = DbManager::getConnection()->prepare($query); $query->bindParam(':company_id', $companyId, PDO::PARAM_INT); $query->bindParam(':vessel_id', $vesselId, PDO::PARAM_INT); $query->execute(); } echo "Seed for '" . Company::$tableName . "' and '" . Vessel::$tableName . "' tables complete.\n"; }
/** * @return mixed */ public function run() { $typeIds = $this->typeRepository->getAll([Type::$columnPrimaryKey]); $vesselIds = $this->vesselRepository->getAll([Vessel::$columnPrimaryKey]); foreach ($vesselIds as $vesselId) { $vesselId = $vesselId[Vessel::$columnPrimaryKey]; $typeId = $this->faker->randomElement($typeIds)[Type::$columnPrimaryKey]; $query = "UPDATE `" . App::getDbName() . "`.`" . Vessel::$tableName . "` " . "SET `" . Vessel::$columnTypeId . "`= :type_id " . "WHERE `" . Vessel::$columnPrimaryKey . "` = :vessel_id"; $query = DbManager::getConnection()->prepare($query); $query->bindParam(':type_id', $typeId, PDO::PARAM_INT); $query->bindParam(':vessel_id', $vesselId, PDO::PARAM_INT); $query->execute(); } echo "Seed for '" . Vessel::$tableName . "' and '" . Type::$tableName . "' tables complete.\n"; }
/** * Reverse the migrations */ public function down() { $tableDeleted = $this->db->getConnection()->prepare("DESCRIBE `" . App::getDbName() . "`.`" . $this->getTableName() . "`")->execute(); if ($tableDeleted === false) { return; } $this->db->getConnection()->prepare("SET FOREIGN_KEY_CHECKS=0")->execute(); $this->db->getConnection()->prepare("TRUNCATE TABLE `" . App::getDbName() . "`.`" . $this->getTableName() . "`")->execute(); $this->db->getConnection()->prepare("DROP TABLE IF EXISTS `" . App::getDbName() . "`.`" . $this->getTableName() . "`")->execute(); $this->db->getConnection()->prepare("SET FOREIGN_KEY_CHECKS=1")->execute(); if ($tableDeleted === false) { echo "Error: Unable to delete '" . $this->getTableName() . "'.\n"; } else { echo "'" . $this->getTableName() . "' table destroyed.\n"; } }
/** * Run the migrations * @return mixed */ public function up() { $query = "CREATE TABLE `" . App::getDbName() . "`.`" . Type::$tableName . "` (" . "`" . Type::$columnPrimaryKey . "` INT NOT NULL AUTO_INCREMENT," . "`" . Type::$columnName . "` VARCHAR(45) NOT NULL," . "PRIMARY KEY (`" . Type::$columnPrimaryKey . "`) ," . "UNIQUE INDEX `" . Type::$columnName . "_UNIQUE` (`" . Type::$columnName . "` ASC));"; $this->db->getConnection()->prepare($query)->execute(); echo "Table '" . Type::$tableName . "' created.\n"; }
/** * @test */ public function it_returns_db_error_mode() { $randomDbErrorMode = "random-db-error-mode"; putenv("PDO_ERROR_MODE={$randomDbErrorMode}"); $this->assertSame($randomDbErrorMode, App::getPDOErrorMode()); }
/** @test */ public function it_returns_absolute_url() { $html = new Html(); $this->assertSame(App::getBaseURL() . "/random-url", $html->url("random-url")); }
protected function tearDown() { unset($_SERVER['REQUEST_URI']); unset($_SERVER['SERVER_NAME']); App::setBaseUrl(null); }
/** * @param $src */ public function script($src) { echo "<script src='" . App::getBaseURL() . "/{$src}'></script>"; }