zf3 Custom database factory doesn't want to apply - php

I have strange behavior with database custom factory.
For example I want to use BjyProfiler and create 1 config like this:
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=framework;host=localhost',
'username' => 'root',
'password' => '',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Database\Adapter\MainAdapterFactory',
),
),
So to use Zend\Db I added the module in modules.config.php "Zend\Db"(otherwise I get exceptions). The problem is that when I want to get "Zend\Db\Adapter\Adapter", it never go through "Database\Adapter\MainAdapterFactory" and I don't know why... It use some default Adapter. I triend to put factory declaration in global.php, local.php and it doesn't work. Why is this happening? In zf2 this code is ok...
I use composer if that matters.
Update: In my final config I have:
'service_manager' =>
array (size=5)
'aliases' =>
array (size=11)
...
'Zend\Db\Adapter\Adapter' => string 'Zend\Db\Adapter\AdapterInterface' (length=32)
...
'factories' =>
array (size=19)
...
'Zend\Db\Adapter\AdapterInterface' => string 'Zend\Db\Adapter\AdapterServiceFactory' (length=37)
...
'Zend\Db\Adapter\Adapter' => string 'Database\Adapter\Factory\MainAdapterFactory' (length=43)
'abstract_factories' =>
array (size=3)
...
1 => string 'Zend\Db\Adapter\AdapterAbstractServiceFactory' (length=45)
...
...
I don't know from where it comes alias 'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterInterface' but I think this is the problem.

The 'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterInterface' come from the framework.
I suggested you to extends the original 'Zend\Db\Adapter\Adapter' with your own adapter (let's call it MyAdapter), and use your custom adapter:
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\MyAdapter' => 'Database\Adapter\MainAdapterFactory',
),
),

Related

Secondary database connection with Doctrine 2 and ZF2

I am trying to add a extra connection to my doctrine configuration. my orm_default connection works perfectly fine, and now I'm trying to add a new module with its own Doctrine configuration (mostly learning purposes, buts its rather annoying that I can't get it to work).
The module is called Frontpage, and all relevant code is in this one, except for username/password details that resides in local.php...
My error is
Zend\ServiceManager\Exception\ServiceNotCreatedException
An exception was raised while creating "doctrine.entitymanager.orm_hosts"; no instance returned
Also further down the stacktrace (the last exception) which I think is relevant, but don't know how to fix...
Zend\Stdlib\Exception\BadMethodCallException
The option "hydration_cache" does not have a matching setHydrationCache setter method which must be defined
Here is my module config (relevant parts) file:
'doctrine' => [
'connection' => [
'orm_hosts' => [
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => [
'host' => '127.0.0.1',
'port' => '3306',
'dbname' => 'hosts',
],
],
],
'entitymanager' => array(
'orm_hosts' => array(
'connection' => 'orm_hosts',
'configuration' => 'orm_hosts'
)
),
'configuration' => array(
'orm_hosts' => array(
'driver' => 'orm_hosts',
'generate_proxies' => true,
'proxy_dir' => 'data/DoctrineORMModule/Proxy',
'proxy_namespace' => 'DoctrineORMModule\Proxy',
'filters' => array(),
'metadata_cache' => 'array',
'query_cache' => 'array',
'result_cache' => 'array',
//'hydration_cache' => 'array',
)
),
'driver' => array(
'orm_hosts' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\DriverChain',
'drivers' => array(
'Common\Entity' => 'Hosts_Driver'
)
),
'Hosts_Driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(
__DIR__ . '/../src/Common/Entity'
)
),
),
'eventmanager' => array(
'orm_hosts' => array()
),
'sql_logger_collector' => array(
'orm_hosts' => array(),
),
'entity_resolver' => array(
'orm_hosts' => array()
),
],
And my Module.php's getServiceConfig():
public function getServiceConfig()
{
return array(
'factories' => array(
'doctrine.connection.orm_hosts' => new Service\DBALConnectionFactory('orm_hosts'),
'doctrine.configuration.orm_hosts' => new Service\ConfigurationFactory('orm_hosts'),
'doctrine.entitymanager.orm_hosts' => new Service\EntityManagerFactory('orm_hosts'),
'doctrine.entity_resolver.orm_hosts' => new Service\EntityResolverFactory('orm_hosts'),
'doctrine.sql_logger_collector.orm_hosts' => new Service\SQLLoggerCollectorFactory('orm_hosts'),
'doctrine.driver.orm_hosts' => new \DoctrineModule\Service\DriverFactory('orm_hosts'),
'doctrine.eventmanager.orm_hosts' => new \DoctrineModule\Service\EventManagerFactory('orm_hosts'),
'DoctrineORMModule\Form\Annotation\AnnotationBuilder\orm_hosts' => function(\Zend\ServiceManager\ServiceLocatorInterface $sl) {
return new \DoctrineORMModule\Form\Annotation\AnnotationBuilder($sl->get('doctrine.entitymanager.orm_hosts'));
},
),
);
}
And here is my getEntityManager() in IndexController that fails
/**
* #return array|EntityManager|object
*/
public function getEntityManager() {
if (NULL === $this->em) {
/** #var \Doctrine\ORM\EntityManager $em */
$em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_hosts');
//$em = $this->getServiceLocator()->get('doctrine')->getManager("orm_hosts");
$this->em = $em;
}
return $this->em;
}
Any help will be gratly appreciated :)
Best regards
Richard
Okay, so I still don't know what is wrong with the above code, but if I remove
'Hydration_cache' => 'array'
In the doctrine configuration from my module config, it actually works! Still, if anyone want's to explain what happens, I would appreciate to know more :)

Zend Framework 2 + Doctrine 2 Custom configuration with YML mapping not working

I am trying a different configuration to working with a new Project.
What I want to do is to create a database by writing sql on hand.
After it I want to do a convert-mapping from database to "YML" instead of php annotations.
So, to finish it, I want to convert those YML mapping information to Doctrine Entity, inside a ZF2 Module.
I am Using in composer:
"doctrine/doctrine-orm-module" : "0.7.0",
"doctrine/doctrine-module" : "0.7.*",
In global.php configuration
'doctrine' => array(
'connection' => array(
// default connection name
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'host' => 'localhost',
'port' => '3306',
'user' => 'root',
'password' => '****',
'dbname' => 'gear',
'charset' => 'utf8'
),
)
)
),
On the Target Module
'doctrine' => array(
'driver' => array(
/* This is where you can change the Mapping Driver */
'orm_default' => array(
'drivers' => array(
'Application\Entity' => 'application_entities_yaml'
),
),
'application_entities_yaml' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\YamlDriver',
'paths' => array(__DIR__ . '/../src/' .__NAMESPACE__. '/Yml')
),
),
),
I am looking to use a custom place to put the YML annotations, on a ZF2 Action I use a exec thats generate this command:
vendor/doctrine/doctrine-module/bin/doctrine-module orm:convert-mapping --namespace="Gear\\Entity\\" --force --from-database yml module/Gear/src/Gear/Yml
Who saves correcty the YML mapping data into the folder module/Gear/src/Gear/Yml
It is exactly the path that I put into "application_entities_yaml" in the module config file.
But when i try to finally create the Entitys to get the work done, with this command:
vendor/doctrine/doctrine-module/bin/doctrine-module orm:generate-entities module/Gear/src/
or with
vendor/doctrine/doctrine-module/bin/doctrine-module orm:generate-entities --generate-annotations=1 module/Gear/src/
I got just:
'No Metadata Classes to process.'
I need to discovery how make Doctrine recognize where i put the metadata classes, to avoid this errors and go on with the project. I'll work with YML entities cuz is the best way to I teach non 'php' programmers write entities by the way. So is important to work with YML.
How to make Doctrine recognize those mapping and convert to entity without problems?
On the Target Module
Just change the Application\Entity to Module\Entity aka Gear\Entity and it works!
'doctrine' => array(
'driver' => array(
/* This is where you can change the Mapping Driver */
'orm_default' => array(
'drivers' => array(
'Gear\Entity' => 'application_entities_yaml'
),
),
'application_entities_yaml' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\YamlDriver',
'paths' => array(__DIR__ . '/../src/' .__NAMESPACE__. '/Yml')
),
),
),

Database could not connect to Zend Framework

I'm sorry I'm completely new to Zend Framework 2 with some tutorials I'm trying to connect my DB connection as follows,
Created a file in
xampp\htdocs\articlemanager\application\configs\autoload\global.php
Inserted the following Zend DB connection code to global.php
<?php
return array(
'service_manager' => array(
'factories' => array(
'Zend\Db\Adapter\Adapter' => 'Zend\Db\Adapter\AdapterServiceFactory',
),
'aliases' => array(
'db' => 'Zend\Db\Adapter\Adapter',
),
),
'db' => array(
'driver' => 'PDO_MYSQL',
'dsn' => 'mysql:dbname=articlemanager;host=localhost',
'username' => 'root',
'password' => '',
'driver_options' => array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
),
),
);
and In the Indexcontroller (\xampp\htdocs\articlemanager\application\controllers\IndexController.php) tested adding $this->db = $this->getServiceLocator()->get('db'); in the indexAction as follows
public function indexAction()
{
$this->db = $this->getServiceLocator()->get('db');
}
When I refresh page it display as
An error occurred
Application error
Can I know what I missed here?
Also I would like to know My Zend Library is in the \xampp\php\Zend and My global.php file in the xampp\htdocs\articlemanager\application\configs\autoload\global.php is it OK?
Why are you using an Alias on 'Db' ?
Try this, your driver name is different from mine.
In addition, please move your username and password to local.php.. so they do not persist in Git projects
global.php
<?php
return array(
'db' => array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=articlemanager;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 example
<?php
return array(
'db' => array(
'username' => 'DB_USERNAME',
'password' => 'DB_PASSWORD',
),
);
database connection problems can be a number of different issues that are not necessarily obvious from looking at the creds / db config info.
have you taken a look at any logs? my application has several different logs setup - PHP error, access log, mysql error log, etc. - depending on your application, you may not have this many as discretely defined, but checking any logs you do have will give you a whole lot more information than just "An error has occurred" :)

Zend Framework Config file JSON

Currently I'm learning ZF2. While going through "Getting start", I see that each config file for module is quite filled with PHP arrays. An example from documentation:
<?php
return array(
'controllers' => array(
'invokables' => array(
'Album\Controller\Album' => 'Album\Controller\AlbumController',
),
),
// The following section is new and should be added to your file
'router' => array(
'routes' => array(
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/album[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Album\Controller\Album',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'album' => __DIR__ . '/../view',
),
),
);
Array with array in it with array. Actually I know, that array is just name of function and it's more like map with key/value pair.
One of the Zend MODS pointed that we can use JSON for config files:
http://framework.zend.com/manual/2.0/en/user-guide/routing-and-controllers.html#comment-696979913
Does anyone can provide example for beginner? I'd really prefer to use JSON format for those file configs instead of arrays/map, but I couldn't find it on ZF homepage. Or maybe I shouldn't do it?
I would try modifying the getConfig() function inside your Module.php file:
return \Zend\Config\Factory::fromFile(__DIR__ . '/config/module.config.json', false);

ZfcUser setup issue

I am trying to setup ZfcUser as described in README.md on the github page.
I understand all but this step in Post-Install: Zend\Db;
2. Now, specify the DI alias for your PDO connection in ./configs/autoload/module.zfcuser.config.php, under the 'pdo' setting. If you created the ./config/autoload/database.config.php file in the previous step, the alias you'll specify is 'masterdb'.
Can anyone describe me what exactly to do?
Thanks very much.
Manage to fix this by updating the parameter name in module.config.php for ZfcUser. Corrected from
'zfcuser_user_tg' => array(
'parameters' => array(
'table' => 'user',
'adapter' => 'zfcuser_zend_db_adapter',
),
),
'zfcuser_usermeta_tg' => array(
'parameters' => array(
'table' => 'user_meta',
'adapter' => 'zfcuser_zend_db_adapter',
),
),
to
'zfcuser_user_tg' => array(
'parameters' => array(
'tableName' => 'user',
'adapter' => 'zfcuser_zend_db_adapter',
),
),
'zfcuser_usermeta_tg' => array(
'parameters' => array(
'tableName' => 'user_meta',
'adapter' => 'zfcuser_zend_db_adapter',
),
),
table => corrected to tableName
I had the same problem. The solution was to uncomment line 15 in module.zfcuser.config.php and not use the 'masterdb' ...
'zend_db_adapter' => 'Zend\Db\Adapter\Adapter',
See support issue ...
https://github.com/ZF-Commons/ZfcUser/issues/42

Categories