Using MS SQL with Symfony 3 - php

Hi there I need some help with Symfony 3 and a MS SQL Server:
I am using Symfony 3 and want to connect to a MS SQL Server, I've done some research and found out that it is not supported by default but there are some Bundles available to use.
After trying some of them I found one Bundle which partially worked for me (realestateconz/mssql-bundle) but I get an error Message everytime I try to query the DB (I formatted it to be more readable):
Uncaught PHP Exception Doctrine\DBAL\DBALException: An exception occurred while executing
'SELECT
t0.id AS id_1,
t0.username AS username_2
FROM user t0
WHERE t0.username = ?' with params ["testusername"]:
SQLSTATE[HY000]: General error: 156 General SQL Server error:
Check messages from the SQL Server [156] (severity 15) [(null)]
The codeline looks like this
$user = $em->getRepository('AppBundle:User')->findBy(array('username' => $username));
My doctrine and symfony settings looks like this:
#config.yml
doctrine:
dbal:
driver_class: Realestate\MssqlBundle\Driver\PDODblib\Driver
host: "%database_host%"
#port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
#charset: UTF8
#parameters.yml
parameters:
database_host: myserver
database_name: database #without schema
database_user: user
database_password: pw
Port and Charset in config.yml are commented out because I found something here on stackoverflow about doctrine using MySQL when port and charset are defined (can't remember where exactly though).
In the parameters.yml I can not add the schema for the DB (full path to table "User" is database.web.[User]) because it will run in another Error.
The Webserver runs Ubuntu 16.04 with apache2, php5.6 (including the package php5-sybase) and I use freetds to connect to the MSSQL-Server (manually connecting works).
Here are my FreeTDS settings:
[MYSERVER]
host = myserveradress
port = 1433
tds version = 8.0
client charset = UTF-8
text size = 20971520
As far as I understood it, Doctrine generates the wrong SQL-Query from the single line of code (MySQL-Syntax instead of MSSQL-Syntax), so what can I do to fix this? Or is there another way of successfully connecting and querying a MSSQL-Server on Linux with Symfony 3?

I found the solution myself, you need to give Doctrine the exact Table name for your Entities in the Syntax the SQL-Server uses.
For the annotation Format Symfony uses it would look like this:
/** Annotation for your Classfile
* ExampleClass
*
* #ORM\Table(name="[ExampleClass]") <-- Square Brackets for MSSQL
* #ORM\Entity
*/
After Changing the Tablename to MSSQL Format everything worked as expected.

Related

symfony pdo_pgsql doctrine:database:create does not use parameters

I use Symfony 3.1.5 and I am trying to run a simple
php bin/console doctrine:database:create
as it is described in the documentation.
My parameters.yml
parameters:
database_host: hhvm_db_1
database_port: 5432
database_name: dbname
database_user: dbuser
database_password: mypassword
My config.yml
doctrine:
dbal:
driver: pdo_pgsql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
I get the following error messages:
[Doctrine\DBAL\Exception\DriverException]
An exception occured in driver: [32681]: could not translate host name "hhvm_db_1
port=5432 dbname=postgres " to address: Name or service not known
[Doctrine\DBAL\Driver\PDOException]
[32681]: could not translate host name "hhvm_db_1 port=5432 dbname=postgres " to a
ddress: Name or service not known
[PDOException]
[32681]: could not translate host name "hhvm_db_1 port=5432 dbname=postgres " to a
ddress: Name or service not known
The error message looks odd to me, because it states dbname=postgres while I have a different database specified in my parameters.yml. I tried to change it directly in the config.yml, but that does not change a thing. Database host and port are taken from my parameters.yml though.
I suppose doctrine is just using default parameters for dbname, as it cannot parse my parameters for some reason. What could be the problem here?
I don't think this is really the issue, but I am running symfony in a docker container with hhvm. hhvm_db_1 in the above error message is the second container running Postgres. When I check the connection with vanilla PHP it works and I can connect to the database. Thus, the host is accessible from my web container.
I found that my system is having problems with the dsn string containing spaces instead of semicolons.
Does not work:
pgsql:host=hhvm_db_1 port=5432 dbname=postgres
Works fine:
pgsql:host=hhvm_db_1;port=5432;dbname=postgres
My initial problem was also solved by that. The DBAL driver for PDOPgSQL is using postgres as the default database to check the connection.

Symfony with MSSQL (sqlsrv) results in timeout

I am trying to connect Symfony 2.8 to Microsoft SQL Server 2012 by using sqlsrv extension (no pdo sqlsrv, because for php7 - which I am using - there is currently only non pdo).
Trying to run cache clear or access the site in browser results in
"PDO::__construct(): MySQL server has gone away".
When I run the doctrine schema update command, everything works fine and the tables are being generated. Does anyone know how to fix the error? (I don't even know why it says Mysql server gone away when I try to connect on microsoft sql..)
Config:
# Doctrine Configuration
doctrine:
dbal:
default_connection: default
connections:
default:
driver: sqlsrv
host: testhost
dbname: testdb
user: testuser
password: testpw
mapping_types:
timestamp: string
I am looking for a solution since Friday but was not able to find one. I tried to select some entries from a seperate (non symfony) php skript, and there I got the results and no timeout. Why is Symfony / Doctrine giving me this strange error?
you can try to launch this command for doctrine :
php app/console doctrine:cache:clear-metadata
php app/console doctrine:cache:clear-query
php app/console doctrine:cache:clear-result
Also perhaps upgrade and downgrade your doctrine bundle for restore a correct and initial version of bundle for your project.

Connect Doctrine to a MS SQL Database

Just before i get started, I have been trying to figure this out and have clicked nearly every google link there is and I have read nearly all the other questions on this. But I'm stuck because the bundles that are being suggested for this are out of date.
I am creating a website. Tt is a symfony2 application that is being hosted on Microsoft azure. What I want to do is be able to use doctrine of course to fetch and create users to and from the database.
Now from what I've been reading, to connect to this type of database, I have to use the driver called "PDO_dblib".
I have installed this bundle as it seems to be the only one thats still active, don't quote me on that.
https://github.com/realestateconz/MssqlBundle
Now, I installed this in my vendor folder, is this the correct place to store it? Like so:
Project/
app/
src/
vendor/
realestate/
Of course i added the bundle to the AppKernel like so:
new Realestate\MssqlBundle\RealestateMssqlBundle();
and last but not least here is what i have in my config file:
doctrine:
dbal:
default_connection: default
connections:
default:
driver_class: Realestate\MssqlBundle\Driver\PDODblib\Driver
host: %database_host%
dbname: %database_prefix%%database_name%
user: %database_user%
password: %database_password%
so what im thinking im doing here is telling doctrine to use this driver? dont see what else it could be.
I have also declared my parameters.yml for the connection settings.
PS: I am doing my Dev on Linux Mint!
before i tried the steps of this bundle i also ran throught this websites steps: https://dunglas.fr/2014/01/connection-to-a-ms-sql-server-from-symfony-doctrine-on-mac-or-linux/
But again it was throwing errors, I'll post the errors I get down below!
So for the record I have installed freetds and php5-sybase.
The errors that I am getting are such:
[Symfony\Component\Debug\Exception\ContextErrorException]
Warning: class_implements(): Class Realestate\MssqlBundle\Driver\PDODlib\Driver does not exist and could not be loaded
and also this when i try to do a:
php app/console doctrine:database:create
(I do have my entity set up)
but i get the following error from the command of create:
[Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException]
You have requested a non-existent parameter "database_prefix". Did you mean one of these: "database_port", "database_user"?
I have been trying to get this working for the past few days and any help would be fantastic! Any more information needed feel free to ask of course!
According your error code:
[Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException]
You have requested a non-existent parameter "database_prefix". Did you mean one of these: "database_port", "database_user"?
It seems that you miss configuring the parameter “database_prefix”.
If the tables in your database have the unitized prefix e.g. “core_user”,”core_tasks”… You can configure this parameter in the file parameters.yml if not, you can just remove the parameter % database_prefix % in the dbname line in file config.yml.
Here are my code snippets for your reference:
Config.yml:
doctrine:
dbal:
default_connection: default
connections:
default:
driver_class: Realestate\MssqlBundle\Driver\PDODblib\Driver
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
parameters.yml:
parameters:
database_host: {your_sql_server_name}.database.windows.net
database_port: 1433
database_name: {database_name}
database_user: {username}
database_password: {password}
And the test query in controller:
$conn = $this->get('database_connection');
$data = $conn->fetchAll('SELECT * FROM Testtable');
var_dump($data);

How to connect to MySQL using SSL on symfony/doctrine

I am trying to connect to MySQL that has SSL enabled.
I am using Symfony2 framework with Doctrine.
In plain PHP, I can achieve this with
$link = mysql_connect("127.0.0.1:3306","test","testpass",true,MYSQL_CLIENT_SSL);
Does anyone know how I can do this in symfony/doctrine?
What is the correct doctrine configuration in config.yml?
UPDATE:
Maybe my question "What is the correct doctrine configuration in config.yml?" is wrong.
So, how do I go about doing this? Where should I start?
Thanks
I have found the answer after a long search and with the help from people from doctrine chat room.
This is the dbal configuration that works on PHP > 5.3.7
It uses three PDO Constants which are not available to PHP prior to 5.3.7
In standard PDO connection:
$conn = new PDO("mysql:host=localhost;port=3307;database=dbname", "user1", "password1",
array(
1010 => '/path/to/certs/priv_key.pem',
1011 => '/path/to/certs/pub_cert.pem',
1012 => '/path/to/certs/ca_cert.pem',
)
);
If trying the above code gives you an error, it is possible that your PHP version is < 5.3.7 then you are unlikely to be able to use PDO with SSL.
Now the solution to the DBAL configuration in config.yml
doctrine:
dbal:
default_connection: default
connections:
default:
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
password: %database_password%
charset: UTF8
mapping_types:
enum: string
options:
1010 : %priv_key%
1011 : %pub_cert%
1012 : %ca_cert%
default2: # second connection ...
orm:
# orm configuration here ....
Hope this helps anyone who are trying to connect using SSL. As a matter of fact, it is recommended to connect using SSL for all your database connection, if it is possible to do so.
Just wanted to point out that the integer values of the following SSL attribute constants are as follows in PHP 5.4.16:
PDO:MYSQL_ATTR_SSL_KEY: 1007
PDO:MYSQL_ATTR_SSL_CERT: 1008
PDO:MYSQL_ATTR_SSL_CA: 1009
They may vary from one version to another, so best to check these values before plugging them into the DBAL confiugration.

Symfony2 / Doctrine2 can't connect to IBM DB2 database using ibm_db2 client

I'm having an issue with Doctrine2 that seems like a bug but I can't find anyone else on Google with the same problem. I'm hoping somewhere here has experienced this problem and knows how to solve it.
Basically I'm trying to connect to a DB2 database. I prefer to use the ibm_db2 client as it's supposed to be better and faster (than PDO_IBM or PDO_ODBC). I've installed the client and tested it. Everything seems to work there. But when I use Doctrine I get the following error:
Notice: Undefined index: protocol in
...[my folders].../vendor/doctrine-dbal/lib/Doctrine/DBAL/Driver/
IBMDB2/DB2Driver.php line 54
So in that file it's looking for $params['protocol'] which seems to have no defaults. So in config.yml I tried this:
# Doctrine Configuration
doctrine:
dbal:
default_connection: default
driver: %database_driver%
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
protocol: TCPIP
But when I do that it complains that protocol is an undefined configuration option (and looking through the DependencyInjection stuff it doesn't appear anywhere in there.)
However: if I hard-code TCPIP into the Driver file where the error occurs ... it all works. This is undesirable since it involves changing the vendor supplied file. Has anyone found a way to properly specify the protocol in configuration?
Eventually you're going to run into licensing issues using ibm_db2 as noted here. PDO or ODBC are going to be your only free ways to go. IBM requires DB Connect to use ibm_db2 db2_connect() stuff.

Categories