Symfony 3, query without Entity but with doctrine

You could use the DBAL to make a plain old query, which will return an array:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\EntityRepository;

class MyRepository extends EntityRepository
{
    public function findSmth()
    {
        $conn = $this->getEntityManager()->getConnection();
        $sql = 'SELECT * FROM my_table';
        $stmt = $conn->prepare($sql);
        $stmt->execute();

        var_dump($stmt->fetchAll());die;
    }
}

QueryBuilder just converts down to DQL, and DQL provides querying capabilities over your object model. So, I think you cannot use QB without entity.


You can add new connection to doctrine config:

config.yml

....
doctrine:
    dbal:
        default_connection: default

        connections:
            default:
                ...
            old_mysql_db
                driver:   pdo_mysql
                host:     127.0.0.1
                port:     3306
                dbname:   dbname
                user:     user
                password: password
    orm:
        ...
...

And use this connection in controllers

...
$conn = $this->getConnection('old_mysql_db');
$qb = $conn->createQueryBuilder(); // \Doctrine\DBAL\Query\QueryBuilder
...

Doctrine connects to DB on first sql-query that uses this DB.