Answer the question
In order to leave comments, you need to log in
How to access the database from a static class in symfony?
I have created a static class inside Symfony (no dependencies of Symfony itself) and I want to work with the database using Doctrine. Also I have table entity in DB and repository for this Entity.
As I understand it, I am missing the ContainerInterface, which is automatically in the controller generated by Symfony, but I do not have a controller.
The manuals tell me that there is such a way:
/** @var FramePagesRepository */
protected $framePages;
public function __construct(FramePagesRepository $framePages) {
$this->framePages = $framePages;
}
Answer the question
In order to leave comments, you need to log in
You can get a connection to the database through DBAL (an abstraction layer that runs on top of PDO).
$config = new \Doctrine\DBAL\Configuration();
$connectionParams = array(
'dbname' => 'mydb',
'user' => 'user',
'password' => 'secret',
'host' => 'localhost',
'driver' => 'pdo_mysql',
);
$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
$conn->createQueryBuilder()->select('*')
->from('users')
->where('id = :id')
->setParameter('id', 1);
User::find()->where(['email' => $email])->one();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question