Connect Doctrine to a MS SQL Database - php

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);

Related

Create Entity Manager in Symfony3?

I have an existing entity in a Symfony3 project that I am trying to add an entity manager to. My doctrine.yml file looks like this
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_mysql
host: localhost
port: 3306
dbname: fullstackdb
user: root
password: root
charset: UTF8
lego:
driver: pdo_mysql
host: localhost
port: 3306
dbname: fullstackdb
user: root
password: root
charset: UTF8
orm:
default_entity_manager: default
entity_managers:
auto_mapping: true
default:
connection: default
mappings:
lego:
connection: lego
mappings:
AppBundle: ~
However anytime I try to access this entity manager through php bin/console doctrine:database:create --connection=lego
, it says the manager does not exist! I'm not sure if I'm missing a step - the only thing I've done to create the manager is to make the yml file.
It doesn't show up when I run bin/console debug:container.
Grateful for any help!
1) You are using the same schema name (database name) on the same host in two different entity managers without schema_filters. This means that when you do an update to one of the entity managers it will attempt to delete the data of the other entity manager.
2) You are asking about a entity manager but in the command you are passing a connection.
3) Run php bin/console debug:container | grep doctrine
This will give you all the doctrine services. Update your question accordingly.
4) Provide the exact command and exact error you get so we can track it down to the origin
5) For a more information run the command in verbose mode adding -v at the end
6) Are you importing your doctrine.yml in your config.yml ?

Sonata: The service "security.authentication.manager" has a dependency on a non-existent service "security.user.provider.concrete.fos_userbundle"

I've correctly setup my Admin bundle in Sonata and followed the instructions up to 2.5.
But upon extending I got the error:
ServiceNotFoundException in CheckExceptionOnInvalidReferenceBehaviorPass.php
line 58: The service "security.authentication.manager" has a dependency on
a non-existent service "security.user.provider.concrete.fos_userbundle"."
I recall setting up a service for the Admin bundle, but the documentation does not call for such for the User. Is there something I missed, I've double checked to see if I fat fingered it to no prevail.
I've dumped my kernel, composer, security and config to this pastebin. Seemed like a mouthful to format on here.
Update: This is my service dump on pastebin
Once I removed the extra provider from the security.yaml file I was able to extend and add the ApplicationSonataUserBundle. Now when I visit the /admin/dashboard route it says it doesn't exist even though when i run route:debug command it shows up on there.
Here is the github of my project and here is the log of the new error I'm having when I login via /login: Prod.log
Every time I run php app/console doctrine:schema:update i get the error:
[Doctrine\DBAL\DBALException]
Unknown column type "json" requested. Any Doctrine type that you use has to
be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list
of all the known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If
this error occurs during database introspection then you might have forgot
to register all database types for a Doctrine Type. Use
AbstractPlatform#registerDoctrineTypeMapping() or have your custom types
implement Type#getMappedDatabaseTypes(). If the type name is empty you might
have a problem with the cache or forgot some mapping information.`
For Doctrine Exception you must add a new Type Json
types:
json: Sonata\Doctrine\Types\JsonType
mentionned in https://sonata-project.org/bundles/notification/master/doc/reference/installation.html
from config.yml
# Doctrine Configuration
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
types:
json: Sonata\Doctrine\Types\JsonType

Symfony2 + Propel: no connection information

I'm trying to connect propel and symfony2 together, but the only thing that I get is this exception:
No connection information in your runtime configuration file for datasource [symfony]
I used the propel bundle which I added to the composer, and I edited the kernel to include the bundle.
My config.yml propel conf looks like that:
propel:
dbal:
driver: mysql
user: root
password: null
dsn: mysql:host=localhost;dbname=symfony;charset=UTF8
options: {}
attributes: {}
What can I be missing? I tried googling, but nothing really solves the issue.
Okay, I solved the issue.
The problem lied in the config.yml file, where the propel dbal data should be stored. I initially put it the way they describe it in the manual, like that:
propel:
dbal:
driver: "%database_driver%"
user: "%database_user%"
password: "%database_password%"
dsn: "%database_driver%:host=%database_host%;dbname=%database_name%;charset=%database_charset%"
According to the manual on both the propel and symfony's sites, it is enough. However, this doesn't seem to be the case for some reason yet unknown to me. What I had to do was to explicitly state the connection and name it precisely - symfony, like that:
propel
dbal:
default_connection: symfony
connections:
symfony:
driver: %database_driver%
user: %database_user%
password: %database_password%
dsn: %database_driver%:host=%database_host%;dbname=%database_name%;charset=UTF8
This way it worked. Sucks to be a junior developer sometimes.

Error create database with shell

I come here for the first time because I have a problem when I want to create my database with doctrine and Symfony. When I want export my classes in my database I have this problem. I think everything is okay, doctrine find my base but can't create it. Also, I created my database named "symfony" in phpmyadmin.
I hope someone can help me, thank you so much :).
php app/console doctrine:database:create
Could not create database for connection named `symfony`
An exception occurred while executing 'CREATE DATABASE `symfony`':
SQLSTATE[HY000]: General error: 1007 Can't create database 'symfony'; database e
xists
Configuration file:
parameters:
database_driver: pdo_mysql
database_host: 127.0.0.1
database_port: null
database_name: symfony
database_user: root
database_password: null
mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: null
mailer_password: null
locale: en
secret: 255a33a57b471045aa29326785659c6b0
database_path: null
# Doctrine Configuration
doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
# if using pdo_sqlite as your database driver, add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# path: "%database_path%"
orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true
To summarize:
The error informs you that a database with the same name was already created. There are three options:
(1) Delete your current database without saving it, using MySQL command line. If you want to delete your database run: DROP database name_database
(2) Delete the database without saving it, using console: C:\wamp\www\Symfony>php app/console doctrine:database:drop
(3) In the case you like to save the content of your current database. Rename you current database. As far as I know you cannot do this directly. This is a work around. First, create a new database using command line: CREATE DATABASE name_old_database;. Second, copy all tables to the new database RENAME TABLE name_database.name_table TO name_old_database.name_table;. Finally,DROP DATABASE name_database
See further info: How do I quickly rename a MySQL database (change schema name)?

Doctrine 2 - How to add custom DBAL driver?

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);

Categories