Silex DoctrineServiceProvider not connecting - doctrine-orm

I'am building application on Silex, and I'am having some problems on very basic stuff. I used example from official documentation for using doctrine service provider, but no matter what i do $app[ 'db' ]->isConnected() returns false. Here is the code
$app = new Application();
$app->register(new DoctrineServiceProvider(), array(
'dbs.options' => array (
'mysql' => array(
'driver' => 'pdo_mysql',
'host' => 'localhost',
'dbname' => 'sevenbet',
'user' => 'root',
'password' => '',
'charset' => 'utf8',
)
),
));

Don't use multi dimension array, if you have one db config to provide, just use :
$app['db.options'] = array (
'driver' => 'pdo_mysql',
'host' => 'localhost',
'dbname' => 'mydb',
'user' => 'root',
'password' => 'root',
'charset' => 'utf8'
);

Related

Unit-testing with CakePhP and PHPUnit

I`m trying to do unit-testing with CakePhP 2.3 and PHPUnit 2.7. I want to test the index function in my customer’s controller.
In my controller I have:
public function index() {
$this->Customer->recursive = -1;
$data = $this->paginate('Customer', array( 'Customer.status'=>'active'));
$this->set('customers', $data);
}
I tried to follow the examples in book.cakephp.org, so I created Fixture class in which I`m importing the Customer schema and all the records.
class CustomerFixture extends CakeTestFixture{
public $import = array('model' => 'Customer', 'records' => true);
}
And finally my test class looks like this:
class CustomersControllerTest extends ControllerTestCase {
public $fixtures = array('app.customer');
public function testIndex() {
$result = $this->testAction('/customers/index');
debug($result);
}
}
When I run my test I have the following error:
Database Error
Error: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '8' for key 'PRIMARY'
Do you have any ideas what can be the problem?
In your database.php in app/Config folder you have to add a $test variable.
For example
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => true,
'host' => 'localhost',
'port' => 3306,
'login' => 'root',
'password' => 'xxxx',
'database' => 'mydatabase',
'encoding' => 'utf8'
);
public $test = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'port' => 3306,
'login' => 'root',
'password' => 'xxxx',
'database' => 'mydatabase_test',
'encoding' => 'utf8'
);
}
Then your unit testing will use the mydatabase_test for testing your code. Because now it uses the default database.

zendframework 2 table gateway And doctrine 2- is it possible to operate both systems at same time

i am using Zendframework 2 . i wonder if it possible to use both table gateway and doctrine 2 in the same application.
below is my configuration. i tried to run it but i noticed that the speed of my application slowed down considrable.
i was wondering if this was beucase there is a conflict between both applications or because i have not configured it properly.
global PHP file
return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=thetable;host=localhost',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter'
=> 'Zend\Db\Adapter\AdapterServiceFactory',
),
),
);
local php
return array(
'db' => array(
'username' => 'user',
'password' => 'password',
),
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'host' => 'localhost',
'port' => '3306',
'user' => 'user',
'password' => 'password',
'dbname' => 'thetable'
)
)
)
),
);

Doctrine- unknown database type enum requested

I am using doctrine 2 within zend framework 2. To generate entities using database table, the console command used is:
php doctrine-module orm:convert-mapping --force --from-database annotation ./export
When i run above command, it throws an error:
Unknown database type enum requested
How to solve this issue?
You can add:
'doctrine_type_mappings' => array(
'enum' => 'string'
)
in your global configuration file located in /config/autoload/global.php.
Example code:
return array(
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' =>'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'host' => 'localhost',
'port' => '3306',
'user' => 'username',
'password' => 'password',
'dbname' => 'DevBrew',
),
// To automatically convert enum to string
'doctrine_type_mappings' => array(
'enum' => 'string'
),
)
)
)
);

ZF2 + Doctrine 2 : Cli Tool has Access Denied

I have a ZF2 2.1.3 app with Doctrine 2.
I installed using composer:
"zendframework/zendframework": "2.*"
,"doctrine/doctrine-orm-module": "dev-master"
A create a file config/autoload/database.local.php with the following:
return array(
'doctrine' => array(
'connection' => array(
// Default connection name
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'host' => 'localhost',
'port' => '3306',
'dbname' => 'zf2-experimental',
'user' => 'root',
'password' => '',
),
),
),
),
);
I create my entity Bar under Foo module (src/Business/Entity/Bar.php) and configure it on module.config.php like this:
'doctrine' => array(
'driver' => array(
__NAMESPACE__ . '_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Business/Entity'),
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Business\Entity' => __NAMESPACE__ . '_driver',
),
),
),
),
Ok! It's exactly at Sam says here!
Then I create a simple Foo entity and run the doctrine cli tool:
c:\wamp\www\zf2-Experimental>.\vendor\bin\doctrine-module.bat orm:validate-schema
[Mapping] OK - The mapping files are correct.
[PDOException]
SQLSTATE[28000] [1045] Access denied for user 'username'#'localhost' (using password: YES)
orm:validate-schema
c:\wamp\www\zf2-Experimental>
Looks like the cli tool can get my connection parameters in my config/autoload/database.local.php!
On Application index I tested my config:
$config = $this->getServiceLocator()->get('config');
\Zend\Debug\Debug::dump($config['doctrine']['connection']['orm_default']);
And this returned:
array (size=4)
'configuration' => string 'orm_default' (length=11)
'eventmanager' => string 'orm_default' (length=11)
'params' =>
array (size=5)
'host' => string 'localhost' (length=9)
'port' => string '3306' (length=4)
'user' => string 'root' (length=4)
'password' => string '' (length=0)
'dbname' => string 'zf2-experimental' (length=16)
'driverClass' => string 'Doctrine\DBAL\Driver\PDOMySql\Driver' (length=36)
Someone can, please, help me?! :)
Thanks you all!
Well, I waste a lot of time trying to solve this and finally did.
I forgot to mention that I using ZF1 environment-specific configurations style!
You can see here!
I create my database config file in the following structure (I didn't mention this before, because I thought It wouldn't interfere on the question):
'module_listener_options' => array(
'config_glob_paths' => array(
'config/autoload/{,*.}{global,local}.php',
// Here! My config path is config/autoload/app_env/development/
'config/autoload/app_env/' . (getenv('APPLICATION_ENV') ?: 'production') . '{,*.}.local.php',
),
So, my database configs actually are in config/autoload/app_env/development/database.local.php!
When I copy-paste the file to config/autoload/database.local.php my CLI Tool works!
But WHY? Why the Cli Tool can't look for the config file inside another directory?
There is a way to work with sub-folders?
Thanks for the help!

How to create database in doctrine 2

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