1

For my company I'm working on a Symfony webapp that needs to fetch users and businesses from the database. However I am forced to use an ODBC connection. I went with pdo_odbc and this is working fine from the controller (Connection, queries, fetching, rendering).

But I'm a bit lost on how to use Entities and Repositories if I'm unable to use Doctrine to connect.

So what I'm doing now is connecting to the database, do queries, fetching data and rendering to the views in one controller function. Without using Entities or Repositories, this is obviously not what it should be.

If any of you need more information feel free to comment.

PS: I'm new to Symfony in general, so examples are much appreciated

Kees Sonnema
  • 5,759
  • 6
  • 51
  • 106

1 Answers1

3

We're doing the same stuff on some projects.

We've done the following steps:

  • Write Repositories, but instead of using the Doctrine ORM and QueryBuilder, we just use the PDO connection with PDO statements.

  • We create Models instead of Entites. Every Model has a "setFromDb()" function which receives an array of data. Those informations will be matched to the attributes of the Model.

  • Within services, Controller whatsoever we're only working with the models.

The repositories we've written just are classes that are defined as services and will receive the doctrine connection as constructor injection. Then you can get the connection and work with PDO.

Example for a repository class (note that we just call them repositories because they handle the database queries, but they have nothing to do with the Repository classes from Symfony / Doctrine)

class DemoRepositoryClass
{
    private $connection;

    public function __construct(Registry $doctrine)
    {
        // Get the database connection from doctrine
        $this->connection = $doctrine->getConnection();
    }

    public function test()
    {
        $query = "SELECT * FROM XXX WHERE FOO = 'BAZ";
        $stmt = $this->conncetion->prepare($query);
        // [...] Do as always with PDO;
    }

}

This class will be defined as service in the services.yml

app.demo_repository_class:
    class: AppBundle\Path\DemoRepositoryClass
    arguments: ['@doctrine']
    public: true

With that, you can call the service functions from the controller as example:

// In a controller action/function
$this->get('app.demo_repository_class')->test();
KhorneHoly
  • 4,666
  • 6
  • 43
  • 75
  • That sounds like a good way of handling it, but could you give some examples for the repositories and services? I forgot to add that I'm also new to Symfony in general. – Kees Sonnema Nov 23 '17 at 13:47
  • The connection part is still a bit fague. I tried using the parameters.xml file to add the connection, but that did not work. I'm using the following in my controllers currently `$conn = new PDO('odbc:DSN', 'USER', 'PASS');` – Kees Sonnema Nov 23 '17 at 14:05
  • In our projects, we define the connection in the config with doctrine. If you're using plain PDO, you could just define the connection within the constructor of the repository class and just inject the connection data over the service from the paramters. So, define host and so on in the parameters and inject those instead of the '@dotrine' part in the services.ym – KhorneHoly Nov 23 '17 at 14:25
  • @KeesSonnema alright, no problem. Maybe you'd want to delete some of your comments to reduce noise, I've added the `public: true` part to my answer. – KhorneHoly Nov 24 '17 at 08:30