doctrine 2 create database and tables [duplicate] - php

I would like to create a database with doctrine 2 and zend framework 2.
I tried to use the command line but it doesn't work because first of all I need to be connected to a database.
Here is the command line that I could use :
When I use the command "php doctrine dbal:run-sql CREATE DATABASE TOTO", I receive an error which tells me that I the database that I selected (but I don't want to select any database) is unknown.
Do you have any idea how I can figure out this problem ?
I really appreciate if I not obliged to use phpmyadmin and create it by my own. I'll prefer to use doctrine to make sure that my code is compatible with other kind of database (such as Mysql/Postegre)
Thank you =D

I found the solution.
You just have to specify in your configuration file that the dbname is equals to null.
<?php
return array (
'doctrine' => array (
'connection' =>
array (
'orm_default' =>
array (
'driverClass' => 'Doctrine\\DBAL\\Driver\\PDOMySql\\Driver',
'params' =>
array (
'host' => 'localhost',
'port' => '3306',
'user' => 'root',
'password' => '',
'dbname' => null,
'charset' => 'UTF8',
),
),
'orm_poems' =>
array (
'driverClass' => 'Doctrine\\DBAL\\Driver\\PDOMySql\\Driver',
'params' =>
array (
'host' => 'localhost',
'port' => '3306',
'user' => 'root',
'password' => 'mot de passe',
'dbname' => 'poemsV3',
'charset' => 'UTF8',
),
),
),
),
);
Have a good day everybody =D

Related

Symfony Multiple Databases

I'm not asking you to help me with a code purpose, but for an advice of how to do what I've got to do.
Since a few weeks, i'm developing a web application based on the symfony3 framework, and now, I have to modify what I did because my boss wants me to open the solution on multiple "groups". In fact, the group will correspond with a database which will be the group's content for the application.
I explain with a schema:
Login page -> login / password / group
If GROUP = A, database = db_group_a (for exemple)
If GROUP = B, database = db_group_b (..)
I dont know if it's clear, but here's what I have to do, and I really dont know how to do it with Symfony (In simple PHP, I would do it quite simply, but Symfony makes me ask you). If you could help me, it would be awesome.
Thanks!
In symfony you can define multiple databases like this:
$container->loadFromExtension('doctrine', array(
'dbal' => array(
'default_connection' => 'default',
'connections' => array(
'default' => array(
'driver' => '%database_driver%',
'host' => '%database_host%',
'port' => '%database_port%',
'dbname' => '%database_name%',
'user' => '%database_user%',
'password' => '%database_password%',
'charset' => 'UTF8',
),
'group_a' => array(
'driver' => '%database_driver2%',
'host' => '%database_host2%',
'port' => '%database_port2%',
'dbname' => '%database_name2%',
'user' => '%database_user2%',
'password' => '%database_password2%',
'charset' => 'UTF8',
),
'group_b' => array(
'driver' => '%database_driver2%',
'host' => '%database_host2%',
'port' => '%database_port2%',
'dbname' => '%database_name2%',
'user' => '%database_user2%',
'password' => '%database_password2%',
'charset' => 'UTF8',
),
),
),
Now, once you logged in and found out, what group is the user privileged to, you can set that in your session and add as a parameter to queries similar to this:
$allowed_db = 'group_a';
$customers = $this->get('doctrine')
->getRepository('AcmeCustomerBundle:Customer', $allowed_db)
->findAll();

Doctrine2: How to change from MySQL to Oracle database

Initially, I started to develop a web application using Zend Framework 2 and Doctrine2 with MySQL but the customer wants that the database would be Oracle. So, I have to change the database from MySQL to Oracle.
To do that, I have modified my doctrine.local.php file from this version
return array(
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' =>'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'host' => 'localhost',
'port' => '3306',
'user' => 'user_gnsys',
'password' => '******',
'dbname' => 'gnsys',
),
'doctrine_type_mappings' => array(
'enum'=>'string'
)
),
)
)
);
To this other version ...
return array(
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\OCI8\Driver',
'params' => array(
'host' => 'localhost',
'port' => '1521',
'dbname' => 'GNSYS',
'driver' => 'oci8',
'user' => 'c##gnsys',
'password' => '********',
'servicename' => 'demo31',
),
'doctrine_type_mappings' => array(
'enum'=>'string'
)
),
)
)
);
Next, I try to validate my schema using the next command:
josecarlos#R2D2:~/Workspace/Web/gnsys$ php public/index.php orm:validate-schema
And I've got the next exit ...
How can I remove all these "Notice" that I've got on the exit? Have I forget to update another file?
I fixed the problem installing oci8 library.

Kohana 3.3 Database::instance('name') not working

I have an issue with Kohana 3.3 and using different database configurations.
I have a config/database.php with 'default' config and 'other' like this:
return array
(
'default' => array
(
'type' => 'MySQL',
'connection' => array(
'hostname' => 'localhost',
'database' => 'database-one',
'username' => 'root',
'password' => 'password',
'persistent' => FALSE,
),
'table_prefix' => '',
'charset' => 'utf8',
'caching' => FALSE,
),
'other' => array
(
'type' => 'MySQL',
'connection' => array(
'hostname' => 'localhost',
'database' => 'database-two',
'username' => 'root',
'password' => 'password',
'persistent' => FALSE,
),
'table_prefix' => '',
'charset' => 'utf8',
'caching' => FALSE,
));
But in a Controller or Model when trying to use:
Database::instance('other');
Kohana will still use the 'default' configuration. What am I doing wrong?
Thanks!
If you would like to change currently used connection by kohana try this:
Database::$default = 'other';
From this line your code will use 'other' connection till you will switch it again to 'default' using same way.
You can also use another DB configuration once when executing the query in simple way:
DB::...->execute('other');
Or if you store your DB instance earlier:
$other = Database::instance('other');
DB::...->execute($other);
By ... I mean your query.
You need to store the connection in a variable, or the default connection will be used.
Documentation

Zend Framework 2: Database config

I'm digging into ZF2, and I've run into some confusion on how to use Zend\Config with Zend\Db to manually set up a connection.
In different places in the manual, there are db configs in different formats.
This one shows a flat array as the config format:
https://packages.zendframework.com/docs/latest/manual/en/modules/zend.db.adapter.html
$adapter = new Zend\Db\Adapter\Adapter(array(
'driver' => 'Mysqli',
'database' => 'zend_db_example',
'username' => 'developer',
'password' => 'developer-password'
));
While this one shows a nested format:
https://packages.zendframework.com/docs/latest/manual/en/modules/zend.config.introduction.html
$configArray = array(
'database' => array(
'adapter' => 'pdo_mysql',
'params' => array(
'host' => 'db.example.com',
'username' => 'dbuser',
'password' => 'secret',
'dbname' => 'mydatabase'
)
)
);
What I expect to happen is that I can call for a new db adapter like so, but this throws exceptions:
$config = new Zend\Config\Config(
array(
'db' => array(
'adapter' => 'Mysqli',
'params' => array(
'host' => 'db.example.com',
'username' => 'dbuser',
'password' => 'secret',
'dbname' => 'mydatabase'
)
)
)
);
$adapter = new Zend\Db\Adapter\Adapter($config->db);
What I end up having to do is:
$config = new Zend\Config\Config(
array(
'db' => array(
'driver' => 'Mysqli',
'host' => 'db.example.com',
'username' => 'dbuser',
'password' => 'secret',
'database' => 'mydatabase'
)
)
);
$adapter = new Zend\Db\Adapter\Adapter($config->db->toArray());
Is there a better way of achieving what I'm trying to achieve without having to resort to the service manager?
Ignore the example from the Zend Config introduction page, that's just showing how to make a config object from a PHP array, the structure of the array isn't meant to show anything in particular.
Since you don't want to use the service manager, you need to pass the parameters to the adapter class in the structure it expects. It expects an array, a config object won't work. You've worked out what the structure of the array is, so that's what you need to use.
I think this page in the docs: http://framework.zend.com/manual/2.3/en/tutorials/tutorial.dbadapter.html (the "Basic setup" section) gives a better explanation of the service manager approach, which is how I'd do it in an MVC app at least.

FuelPHP - php oil refine migrate PDO Error

I'm trying to setup my fuelphp on ubuntu12, nginx in a development environment.
Everything was working for me except when I try to do php oil refine migrate.
I was faced with the following error message:
Error - invalid data source name in COREPATH/classes/database/pdo/connection.php on line 94
My development/db.php:
return array(
'default' => array(
'connection' => array(
'dsn' => 'mysql:host=localhost;dbname=fuel_intro',
'username' => 'root',
'password' => '',
),
),
);
I searched the Internet and fuelphp docs, and still no luck.
Any help will be appreciated.
It seems you're doing database configuration incorrectly. It shouldn't be 'host:localhost', it should be like 'hostname'=>'localhost'. And please use mysql or PDO instead of mysql... (because mysql_* functions are deprecated.
It should be something like:
'default' => array(
'type' => 'mysqli',
'connection' => array(
'hostname' => 'localhost',
'port' => '3306',
'database' => 'fuel_db',
'username' => 'your_username',
'password' => 'y0uR_p#ssW0rd',
'persistent' => false,
'compress' => false,
),
'identifier' => '`',
'table_prefix' => '',
'charset' => 'utf8',
'enable_cache' => true,
'profiling' => false,
),
Take a look at:
http://fuelphp.com/docs/classes/database/introduction.html for further information.

Categories