PhalconPHP 3.0.x : SQL Server Connector

Starting from PhalconPHP 3.0 theย old connector i wrote to allow me to use SQL Server stopped working.

To let me use new versions of PhalconPHP i updated the old classes (both Adapter and Dialect).

You can download them hereย , search for the two files named Mssql3.php

Join the Conversation

23 Comments

  1. Dialect/Mssql3.php in line 239 you have:
    $this->_dialect = new \Airaghi\PhalconPHP\MSQQL\Dialect\Mssql();
    Must be:
    $this->_dialect = new \Airaghi\PhalconPHP\MSSQL\Dialect\Mssql();

  2. Hello Davide

    I’m trying to use your connector but always geht the error “Fatal error: Class ‘Airaghi\PhalconPHP\MSSQL\Adapter\Mssql’ not found in …services.php.
    I tried many different possibilities but nothing worked for me.
    I’m using Phalcon 3.0.1 and PHP 5.6.15 / 5.6.24.

    The “MSSQL” folder is located in app\library and the libraryDir is registered via “registerDirs” (also tried different things like registerNamespaces and registerClasses).

    The config is:
    ‘database’ => array(
    ‘adapter’ => ‘Airaghi\PhalconPHP\MSSQL\Adapter\Mssql’,
    ‘host’ => ‘localhost’,
    ‘username’ => ‘user’,
    ‘password’ => ‘password’,
    ‘dbname’ => ‘db’,
    ‘dialectClass’ => ‘Airaghi\PhalconPHP\MSSQL\Dialect\Mssql’

    The service registration is:

    $di->setShared(‘db’, function () use ($config) {
    $dbConfig = $config->database->toArray();
    $adapter = $dbConfig[‘adapter’];
    unset($dbConfig[‘adapter’]);

    //$class = ‘Phalcon\Db\Adapter\Pdo\\’ . $adapter;
    $class = $adapter;

    $connection = new $class($dbConfig);
    //$connection->setDialect($config->database->dialectClass);
    return $connection;
    });

    What am I doing wrong? Can you provide me an example?

    Thanks.

    Greets, Jens

    1. Got it working now.
      It was my loader, seems like I didn’t the correct way to register the namespaces. The following is working:

      $loader->registerNamespaces(
      array(
      “Airaghi\PhalconPHP\MSSQL\Adapter” => $config->application->libraryDir . “MSSQL/Adapter/”,
      “Airaghi\PhalconPHP\MSSQL\Dialect” => $config->application->libraryDir . “MSSQL/Dialect/”
      ));

      Greets Jens

      1. Hello Jens, i’m glad you found a way to use my classes.

        If you don’t want to add specific rules using “registerNamespaces” you can:
        * create directory app/library/Airaghi/PhalconPHP/MSSQL/Adapter/ and copy into it every adapter file downloaded from my GitHub repository
        * create directory app/library/Airaghi/PhalconPHP/MSSQL/Dialect/ and copy into it every dialect file downloaded from my GitHub repository

        My classes’ full namespace begins with Airaghi\PhalconPHP, so you have to create also those directories to allow PhalconPHP to automatically load the classes.

  3. Hi dear

    php errror:

    Notice: Undefined index: pdoType in I:\wamp64\www\phalcon\app\library\Airaghi\PhalconPHP\MSSQL\Adapter\Mssql.php on line 225

    phalcon error:

    could not find driver

    ———————————–
    php.ini :

    extension=php_sqlsrv_7_nts_x86.dll
    extension=php_pdo_sqlsrv_7_nts_x86.dll
    extension=php_sqlsrv_7_ts_x86.dll
    extension=php_pdo_sqlsrv_7_ts_x86.dll
    extension=php_sqlsrv_7_nts_x64.dll
    extension=php_pdo_sqlsrv_7_nts_x64.dll
    extension=php_sqlsrv_7_ts_x64.dll
    extension=php_pdo_sqlsrv_7_ts_x64.dll

    Thank you for help

    1. in file config.php check that you have:

      ‘database’ => array(
      ‘adapter’ => ‘\Airaghi\PhalconPHP\MSSQL\Adapter\Mssql’,
      ‘host’ => ‘‘,
      ‘username’ => ‘‘,
      ‘password’ => ‘‘,
      ‘dbname’ => ‘‘,
      ‘pdoType’ => ‘sqlsrv’,
      ‘dialectClass’ => ‘\Airaghi\PhalconPHP\MSSQL\Dialect\Mssql’
      )

  4. ‘database’ => [
    /*’adapter’ => ‘Mysql’,
    ‘host’ => ‘localhost’,
    ‘username’ => ‘root’,
    ‘password’ => ”,
    ‘dbname’ => ‘phalcon’,
    ‘charset’ => ‘utf8’,*/
    ‘adapter’ => ‘\Airaghi\PhalconPHP\MSSQL\Adapter\Mssql’,
    ‘host’ => ‘localhost’,
    ‘username’ => ‘phalcon’,
    ‘password’ => ‘123#’,
    ‘dbname’ => ‘phalcon’,
    ‘pdoType’ => ‘sqlsrv’,
    ‘dialectClass’ => ‘\Airaghi\PhalconPHP\MSSQL\Dialect\Mssql’,
    ‘charset’ => ‘utf8’
    ],

    ———–

    $di->setShared(‘db’, function () {
    $config = $this->getConfig();

    //$class = ‘Phalcon\Db\Adapter\Pdo\\’ . $config->database->adapter;
    $class = $config->database->adapter;

    ——-

    old error now show ๐Ÿ™

    1. in my app/config/services.php i have the following “block” used to initialize db connection:


      $di->set('db', function() use ($config) {
      return new \Airaghi\PhalconPHP\MSSQL\Adapter\Mssql(array(
      'host' => $config->database->host,
      'username' => $config->database->username,
      'password' => $config->database->password,
      'dbname' => $config->database->dbname,
      'pdoType' => $config->database->pdoType,
      'dialectClass' => $config->database->dialectClass
      ));
      });

      in my app/config/services.php i have the following “block” used to initialize classes autoloading :


      $loader->registerDirs(
      array(
      $config->application->controllersDir,
      $config->application->libraryDir,
      $config->application->modelsDir
      )
      )->register();

      and in my app/config/config.php i have the following “block” used to set paths


      [...]
      'application' => array(
      'controllersDir' => __DIR__ . '/../../app/controllers/',
      'modelsDir' => __DIR__ . '/../../app/models/',
      'viewsDir' => __DIR__ . '/../../app/views/',
      'pluginsDir' => __DIR__ . '/../../app/plugins/',
      'libraryDir' => __DIR__ . '/../../app/library/',
      'cacheDir' => __DIR__ . '/../../app/cache/',
      'baseUri' => '/',
      )
      [...]

      1. It means this will not work on 2012? Hmmm I hope you can provide soon, will subscribe on this. Thanks!

        1. You can try use the classes anyway on a testing machine, at the moment i don’t have any sql server 2012 instance so i can’t test this integration.
          If you decide to test it let me know how it goes

          1. I tried 2005 and 2012 both not working. I am not sure if I missed some config but I am receiving the same error above. “could not find driver” I am using WAMP Server too. Do I need to install such components? Or this should would on IIS? Thanks

          2. Sorry I overlooked the sqlsrv driver version. Just to update you, the driver works on PHP7 and SQL 2012 ๐Ÿ™‚ and I manage to pull data from db. Thank You!

  5. could not find driver Error is visible : PHP version is 5.6.16

    Please could you tell me how it could work

    1. I can’t see the error in your message … could you tell me more about it ?

  6. Your Tutorial is Amazing but I am stuck here

    I am Getting this error on Ubuntu 16.4 PHP 7 Phalcon 3.x

    SQLSTATE[07002]: [Microsoft][ODBC Driver 13 for SQL Server]COUNT field incorrect or syntax error

    1. It seems there is an error in the query, could you share the PHP code you have written ?

      1. Thanks Buddy I resolved it. It was query error ‘`’ this sign is not supported in SQL Server.
        Your MSSQL Code was Too Brilliant . Hats off to you buddy .

Leave a comment