Fix database connection

This commit is contained in:
Starbeamrainbowlabs 2019-01-15 16:37:46 +00:00
parent 57ec980b7d
commit f66083575a
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
4 changed files with 15 additions and 10 deletions

View file

@ -28,11 +28,9 @@ $di_container = $di_builder->build();
$settings = $di_container->get(\SBRL\TomlConfig::class);
// 4: Database
// TODO: Setup the database here
// Done via a static factory method & PHP-DI
// 5: Action

View file

@ -3,6 +3,8 @@
use Psr\Container\ContainerInterface;
use SBRL\TomlConfig;
use AirQuality\Database;
use AirQuality\Repositories\IDeviceRepository;
use AirQuality\Repositories\MariaDBDeviceRepository;
use AirQuality\Repositories\IMeasurementDataRepository;
@ -23,8 +25,5 @@ return [
IDeviceRepository::class => DI\autowire(MariaDBDeviceRepository::class),
IMeasurementDataRepository::class => DI\autowire(MariaDBMeasurementDataRepository::class),
IMeasurementTypeRepository::class => DI\autowire(MariaDBMeasurementTypeRepository::class),
\SBRL\SessionManager::class => DI\factory([ \SBRL\SessionManager::class, "get_instance" ])
IMeasurementTypeRepository::class => DI\autowire(MariaDBMeasurementTypeRepository::class)
];

View file

@ -2,6 +2,9 @@
namespace AirQuality;
use \SBRL\TomlConfig;
use Psr\Container\ContainerInterface;
/**
* Holds the main database connection.
*/
@ -20,11 +23,15 @@ class Database
\PDO::ATTR_AUTOCOMMIT => false
];
function __construct(\SBRL\TomlConfig $in_settings) {
function __construct(TomlConfig $in_settings) {
error_log("Created database instance");
$this->settings = $in_settings;
$this->connect(); // Connect automagically
}
public function connect() {
error_log($this->get_connection_string());
$this->connection = new \PDO(
$this->get_connection_string(),
$this->settings->get("database.username"),
@ -44,4 +51,5 @@ class Database
private function get_connection_string() {
return "{$this->settings->get("database.type")}:host={$this->settings->get("database.host")};dbname={$this->settings->get("database.name")};charset=utf8mb4";
}
}

View file

@ -8,8 +8,8 @@
type = "mysql" # MariaDB. MySQL servers might work too, but no promises.
# The host that the database is running on. Both IP addresses & domain names are fine :-)
host = "localhost"
# The host that the database is running on. Both IP addresses & domain names are fine I think :-)
host = "127.0.0.1"
# The database to use.
name = "aq_db"