mirror of
https://github.com/ConnectedHumber/Air-Quality-Web
synced 2024-11-22 06:23:01 +00:00
Fix database connection
This commit is contained in:
parent
57ec980b7d
commit
f66083575a
4 changed files with 15 additions and 10 deletions
4
api.php
4
api.php
|
@ -28,11 +28,9 @@ $di_container = $di_builder->build();
|
||||||
|
|
||||||
$settings = $di_container->get(\SBRL\TomlConfig::class);
|
$settings = $di_container->get(\SBRL\TomlConfig::class);
|
||||||
|
|
||||||
|
|
||||||
// 4: Database
|
// 4: Database
|
||||||
|
|
||||||
// TODO: Setup the database here
|
// Done via a static factory method & PHP-DI
|
||||||
|
|
||||||
|
|
||||||
// 5: Action
|
// 5: Action
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,8 @@
|
||||||
use Psr\Container\ContainerInterface;
|
use Psr\Container\ContainerInterface;
|
||||||
use SBRL\TomlConfig;
|
use SBRL\TomlConfig;
|
||||||
|
|
||||||
|
use AirQuality\Database;
|
||||||
|
|
||||||
use AirQuality\Repositories\IDeviceRepository;
|
use AirQuality\Repositories\IDeviceRepository;
|
||||||
use AirQuality\Repositories\MariaDBDeviceRepository;
|
use AirQuality\Repositories\MariaDBDeviceRepository;
|
||||||
use AirQuality\Repositories\IMeasurementDataRepository;
|
use AirQuality\Repositories\IMeasurementDataRepository;
|
||||||
|
@ -23,8 +25,5 @@ return [
|
||||||
|
|
||||||
IDeviceRepository::class => DI\autowire(MariaDBDeviceRepository::class),
|
IDeviceRepository::class => DI\autowire(MariaDBDeviceRepository::class),
|
||||||
IMeasurementDataRepository::class => DI\autowire(MariaDBMeasurementDataRepository::class),
|
IMeasurementDataRepository::class => DI\autowire(MariaDBMeasurementDataRepository::class),
|
||||||
IMeasurementTypeRepository::class => DI\autowire(MariaDBMeasurementTypeRepository::class),
|
IMeasurementTypeRepository::class => DI\autowire(MariaDBMeasurementTypeRepository::class)
|
||||||
|
|
||||||
|
|
||||||
\SBRL\SessionManager::class => DI\factory([ \SBRL\SessionManager::class, "get_instance" ])
|
|
||||||
];
|
];
|
||||||
|
|
|
@ -2,6 +2,9 @@
|
||||||
|
|
||||||
namespace AirQuality;
|
namespace AirQuality;
|
||||||
|
|
||||||
|
use \SBRL\TomlConfig;
|
||||||
|
use Psr\Container\ContainerInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds the main database connection.
|
* Holds the main database connection.
|
||||||
*/
|
*/
|
||||||
|
@ -20,11 +23,15 @@ class Database
|
||||||
\PDO::ATTR_AUTOCOMMIT => false
|
\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->settings = $in_settings;
|
||||||
|
|
||||||
|
$this->connect(); // Connect automagically
|
||||||
}
|
}
|
||||||
|
|
||||||
public function connect() {
|
public function connect() {
|
||||||
|
error_log($this->get_connection_string());
|
||||||
$this->connection = new \PDO(
|
$this->connection = new \PDO(
|
||||||
$this->get_connection_string(),
|
$this->get_connection_string(),
|
||||||
$this->settings->get("database.username"),
|
$this->settings->get("database.username"),
|
||||||
|
@ -44,4 +51,5 @@ class Database
|
||||||
private function get_connection_string() {
|
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";
|
return "{$this->settings->get("database.type")}:host={$this->settings->get("database.host")};dbname={$this->settings->get("database.name")};charset=utf8mb4";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,8 +8,8 @@
|
||||||
|
|
||||||
type = "mysql" # MariaDB. MySQL servers might work too, but no promises.
|
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 :-)
|
# The host that the database is running on. Both IP addresses & domain names are fine I think :-)
|
||||||
host = "localhost"
|
host = "127.0.0.1"
|
||||||
# The database to use.
|
# The database to use.
|
||||||
name = "aq_db"
|
name = "aq_db"
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue