We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

SQL Server Support

Hi All,

Are there any plans to add MSSQL Server support to phalconphp? I'm keen to even write my own DB Adapter but how do I get started on it? Any advice much appreciated.

Thanks



22.8k

Thanks for the replies I have looked at hthe phalcon-mssql github repo but could not figure out where the place the files. Has anybody gotten this to work?

edited Mar '14

Just use this adapter: https://github.com/fishjerky/phalcon-mssql/blob/master/test/app/library/db/adapter/Mssql.php

  1. Autloload test/app/library. Put this in your composer.json (at the end)

     "autoload": {
        "psr-0" : {
            "Twm\Db\Adapter\Pdo" => "vendor/fishjerky/test/app/library/db/adapter",
        }
    }

Test it though, as i didn't.

  1. Use it (i.e as a regular adapter):
    $db = new \Twm\Db\Adapter\Pdo($connectionDetails);  

Hope that helps you out



22.8k
Accepted
answer
edited Apr '15

Thanks for the feedback everyone. I got this to work. I created a small CRUD application which can be cloned from here https://github.com/chesney/phalconcrud

Kind Regards



80

Hi All Although I tryed to access SQL Server by using above project, I couldn't. The SQL Server is installed to Windows Server 2012 R12 of Vertual Box. I want to access to the server from other vertual box PC(Cent OS), so I put above project to this Cent OS PC. I confirmed that SQL Server Management Studio on Host PC can access to the server by IP address, so IP address or firewall setting is OK.

By debugging, I found that following DB access failed.(indexController::indexAction) $this->view->setVar('users', Users::find());

And I check httpd error log, I found following error.Is it related to my problem ?

[Tue Aug 04 19:23:22 2015] [error] [client ] PHP Stack trace: [Tue Aug 04 19:23:22 2015] [error] [client ] PHP 1. {main}() /var/www/html/phalconcrud-master/public/index.php:0 [Tue Aug 04 19:23:22 2015] [error] [client ] PHP 2. Phalcon\Mvc\Application->handle() /var/www/html/phalconcrud-master/public/index.php:82 [Tue Aug 04 19:23:22 2015] [error] [client ] PHP 3. Phalcon\Dispatcher->dispatch() /var/www/html/phalconcrud-master/public/index.php:82 [Tue Aug 04 19:23:22 2015] [error] [client ] PHP 4. IndexController->indexAction() /var/www/html/phalconcrud-master/public/index.php:82 [Tue Aug 04 19:23:22 2015] [error] [client ] PHP 5. Phalcon\Mvc\Model::find() /var/www/html/phalconcrud-master/app/controllers/IndexController.php:8 [Tue Aug 04 19:23:22 2015] [error] [client ] PHP 6. Phalcon\Mvc\Model\Query->execute() /var/www/html/phalconcrud-master/app/controllers/IndexController.php:8 [Tue Aug 04 19:23:22 2015] [error] [client ] PHP 7. Phalcon\Mvc\Model\Query->_executeSelect() /var/www/html/phalconcrud-master/app/controllers/IndexController.php:8 [Tue Aug 04 19:23:22 2015] [error] [client ] PHP 8. Phalcon\Mvc\Model->getReadConnection() /var/www/html/phalconcrud-master/app/controllers/IndexController.php:8 [Tue Aug 04 19:23:22 2015] [error] [client ] PHP 9. Phalcon\Mvc\Model\Manager->getReadConnection() /var/www/html/phalconcrud-master/app/controllers/IndexController.php:8 [Tue Aug 04 19:23:22 2015] [error] [client ] PHP 10. Phalcon\Mvc\Model\Manager->_getConnection() /var/www/html/phalconcrud-master/app/controllers/IndexController.php:8 [Tue Aug 04 19:23:22 2015] [error] [client ] PHP 11. Phalcon\Di->getShared() /var/www/html/phalconcrud-master/app/controllers/IndexController.php:8 [Tue Aug 04 19:23:22 2015] [error] [client ] PHP 12. Phalcon\Di->get() /var/www/html/phalconcrud-master/app/controllers/IndexController.php:8 [Tue Aug 04 19:23:22 2015] [error] [client ] PHP 13. Phalcon\Di\Service->resolve() /var/www/html/phalconcrud-master/app/controllers/IndexController.php:8 [Tue Aug 04 19:23:22 2015] [error] [client ] PHP 14. {closure:/var/www/html/phalconcrud-master/public/index.php:43-54}() /var/www/html/phalconcrud-master/app/controllers/IndexController.php:8 [Tue Aug 04 19:23:22 2015] [error] [client ] PHP 15. spl_autoload_call() /var/www/html/phalconcrud-master/app/controllers/IndexController.php:44 [Tue Aug 04 19:23:22 2015] [error] [client ] PHP 16. Phalcon\Loader->autoLoad() /var/www/html/phalconcrud-master/app/controllers/IndexController.php:44 [Tue Aug 04 19:23:22 2015] [error] [client ] PHP 17. Phalcon\Loader->autoLoad() /var/www/html/phalconcrud-master/app/controllers/IndexController.php:44

Althogh I think db connection fail, I don't know the detail. I set DB setting to public/index.php like following.

$di->set('db', function(){
    return new \Twm\Db\Adapter\Pdo\Mssql(array(
            'host'          => 'xxxx.xxxx.xxxx.xxxx',
            'port'          => 1433,
            'instance'      => 'WIN-xxxxx',
    'username'  => 'sa',
    'password'  => 'pass',
    'dbname'    => 'test_db',
            'pdoType'       => 'sqlsrv', //sqlsrv
    'dialectClass'  => '\Twm\Db\Dialect\Mssql'
    ), true);//set flag to ,true to enable shared db variable. This increases performance
});

Can I get any detail information from here or phalcon? or anyone find my mistake?

Thanks



22.8k

Hi there,

You need to specify the SQL Server Name and the Instance name and not the IP address.

'host' => 'YOUR_MSSQL_SERVERNAME\YOUR_MSSQL_SERVER_INSTANCE_NAME',

I hope this helps.

Kind Regards