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.
Related
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.
I hope you're all good because i'm not, I have some trouble understand how Symfony 2 does work and I too have a extension problem (Could not find driver).
Firstly, php --ini tells me I have to edit my /etc/php.ini file BUT in my SF2 setting, i'm using Php 7.0.0 from MAMP, using /Applications/MAMP/.../php.ini and /Application/MAMP/../bin/php. So I should be using php 7.0.0 from MAMP shouldn't I ?
My web/config.php tells me "Set "xdebug.max_nesting_level" to e.g. "250" in php.ini* to stop Xdebug's infinite recursion protection erroneously throwing a fatal error in your project."
But i don't have any "xdebug.max" in my /etc/php.ini neither in my /Application/Mamp/../php.ini (I still don't know which one is used ... The one from my MAC or from Mamp ? Meh i'm so lost !)
That's my first problem, knowing which php.ini is used and why it appears to be the one in /etc.
Secondly, i've a "Could not find driver" error when i'm trying to generate bundle with php app/console doctrine:database:create
I searched on stackoverflow but all I found is that i had to check my phpinfo(); which says me that pdo_pgsql is enabled, then I went in /etc/php.ini and MAMP/php.ini to find the ";extension : pdo_pgsql" to uncomment but they already were ... So what's wrong ?
I'm trying to use postgreSQL database with SF2 and edited my config.yml and parameters.yml correctly I think. Here is 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
Well if anyone could help it'd be really cool, i don't understand why i have so much trouble understanding SF2.
I absolutly need to know how to use php from MAMP.
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);
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.
How can I add my custom driver without modifying DriverManager.php in the Doctrine2 core?
I have created a DBAL Driver for pdo_dblib and placed it inside a Symfony2 bundle. This works fine, however I must add my driver to a list of hard-coded drivers in DriverManager.php, otherwise I get the following exception:
Exception
[Doctrine\DBAL\DBALException]
The given 'driver' pdo_dblib is unknown, Doctrine currently supports only the following drivers: pdo_mysql, pdo_sqlite, pdo_pgsql, pdo_oci, oci8, ibm_db2, pdo_ibm, pdo_sqlsrv
Unless I modify DriverManager.php
final class DriverManager
{
private static $_driverMap = array(
'pdo_dblib' => 'Doctrine\DBAL\Driver\PDODblib\Driver', // Added this line
);
}
Here's my config.yml:
# Doctrine Configuration
doctrine:
dbal:
driver: pdo_dblib
driver_class: PDODblibBundle\Doctrine\DBAL\Driver\PDODblib\Driver
You actually can, just leave the driver configuration option completlely out.
All you need to define is the driver_class option. The driver is only used to do an internal lookup for the default driver classes, as long as you provide the class only, it will not fail doing the lookup.
Btw: There is no way (in a complete default setup) to define this in the parameters.ini, you have to change it directly inside the config.yml
Btw: due to another defect (driver falling back to mysql in on specific area), you may not set the charset in the configuration, as it will register an MySql event handler for setting the charset than.
So my final doctrine config based on my mssql_* based implementation looks like the following and works without problems:
# Doctrine Configuration
doctrine:
dbal:
#driver: %database_driver%
driver_class: Doctrine\DBAL\Driver\MsSql\Driver
host: %database_host%
port: %database_port%
dbname: %database_name%
user: %database_user%
password: %database_password%
#charset: UTF8
orm:
auto_generate_proxy_classes: %kernel.debug%
auto_mapping: true
You can use the option driverClass:
$connectionParams = array(
'driverClass' => 'YOUR_CUSTOM_CLASS_DB',
);
$entityManager = \Doctrine\ORM\EntityManager::create($connectionParams, $config);