YamlDriver for Doctrine2 in Zend2 - doctrine-orm

I'm using the DoctrineORMModule to integrate Doctrine2 with Zend2. Everything works just fine when I'm using the AnnotationDriver as described in various examples. However, I cannot get the YamlDriver to work. In my module.config.php I tried:
'doctrine' => array(
'driver' => array(
'ApplicationDriver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/Application/Entity')
),
'YamlDriver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\YamlDriver',
'cache' => 'array',
'extension' => '.dcm.yml',
'paths' => array(__DIR__ . '/../src/Application/Mapping')
),
'orm_default' => array(
'drivers' => array(
'Application\Entity' => 'ApplicationDriver',
'Application\Mapping' => 'YamlDriver'
)
)
)
)
However, the EntityManager cannot find my classes. Cam you give me a working example of how to use yaml with doctrine2 and zend2?

I assume your entities are in namespace Application\Entity: this means your driver should be assigned for that namespace as in following example:
'doctrine' => array(
'driver' => array(
'MyYamlDriver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\YamlDriver',
'cache' => 'array',
'extension' => '.dcm.yml',
'paths' => array(__DIR__ . '/mappings')
),
'orm_default' => array(
'drivers' => array(
'Application\Entity' => 'MyYamlDriver',
)
)
)
)
Basically, the configuration maps a particular named driver to a namespace that you want to use. In this case, MyYamlDriver is assigned to handle any mapping for the namespace Application\Entity

Related

Class was not found in the chain configured namespaces (zend framework 2 and doctrine 2)

I get the following error when I try to use an entity in my controller:
The class 'Application\Entity\User' was not found in the chain configured namespaces in C:\xampp\htdocs\zf2_pr6\vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\MappingException.php:37
My module.config.php configuration includes
'doctrine' => array(
'driver' => array(
'application_entities' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/Application/Entity')
),
),
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMysql\Driver',
'params' => array(
'host' => 'localhost',
'port' => '3306',
'user' => 'root',
'password' => '',
'dbname' => 'zf2',
),
),
),
),
'orm_default' => array(
'drivers' => array(
'Application\Entity' => 'application_entities',
),
),
I've also created a paste bin of other relevant files
Application\Entity\User
Application\Contorller\IndexController
How can I resolve this error add my driver to the chain of configured namespaces?
Your configuration is incorrect. At the moment you have the 'orm_default' config under the 'doctrine' key - Meaning the driver is never added to Doctrine.
You need to have the driver configuration and the 'orm_default' config sitting at the same level, under doctrine/driver.
'doctrine' => array(
'driver' => array(
'application_entities' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/Application/Entity')
),
'orm_default' => array(
'drivers' => array(
'Application\Entity' => 'application_entities',
),
),
)
),

zf2 regex routing

I am struggling with regex routing....
The following urls should be valid:
/shop/api/list
/shop/api/cart
/shop/api/login
/shop/api/details/0123456789
And the details url, the last one, is not really cooperating with the following route:
'child_routes' => array(
'angularlist' => array(
'type' => 'Regex',
'options' => array(
'regex' => '/api/(?<page>login|cart|list|details(?<id>\/[0-9]+))',
'spec' => '/api/%page%[/:%id%]',
'defaults' => array(
'__NAMESPACE__' => 'Shop\Controller',
'controller' => 'Api',
//'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => 'list',
// 'param' => '[0-9]',
),
/* todo ipv elke child toe te voegen 'route' => '/api/[:action]', 'constraints' => array(
'action' => 'list|details|login|dashboard'
)*/
),
),
)
Instead of struggling with less-readable & dirty regex hacks, you can easily implement a Segment route stack like below:
'shop' => array(
'type' => 'Segment',
'options' => array(
'route' => '/shop/api',
'defaults' => array(
'__NAMESPACE__' => 'Shop\Controller',
'controller' => 'Api',
'action' => 'index'
),
),
'may_terminate' => true,
'child_routes' => array(
'angularlist' => array(
'type' => 'Segment',
'options' => array(
'route' => '/:action[/:id]', // id is optional
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'action' => 'index',
),
),
),
)

Zend Framework 2 child regex and literal routing

Based on this stackoverflow question 15077075 i have accomplished that my application has a regex based route so i can pass this to my view and edit action.
app/123 - app controller view action (get by article id)
app/name-of-article - app controller view action (get by article name)
app/123/edit - app controller edit action (article id)
app/name-of-article/edit - app controller edit action (article name)
app/search/{search-string} - app controler search action (currently only accepts a searchstring without spaces and special characters)
This i have accomplished with the code below and is similar to the code in the link above:
'app' => array(
'type' => 'literal',
'options' => array(
'route' => '/app',
'defaults' => array(
'controller' => 'App\Controller\App',
'action' => 'index',
),
),
'may_terminate' => true,
),
'view' => array(
'type' => 'regex',
'options' => array(
'regex' => '/app/(?<view>[a-zA-Z0-9_-]+)',
'defaults' => array(
'controller' => 'App\Controller\App',
'action' => 'view',
),
'spec' => '/app/%view%',
),
'priority' => -1000,
),
'edit' => array(
'type' => 'regex',
'options' => array(
'regex' => '/app/(?<view>[a-zA-Z0-9_-]+)/(?<edit>[a-zA-Z0-9_-]+)',
'defaults' => array(
'controller' => 'App\Controller\App',
'action' => 'edit',
),
'spec' => '/app/%view%/%edit%',
),
'priority' => -1000,
),
I have twoone issue
the first is that the url viewhelper doesn't recognize my routes
$controller = app
$action = recent
$this->url( $controller, array( 'action' => $action ) )
it just prints /app instead of /app/recent,
The same occurs when $action = 'search' or $action = 'new' it only prints /app
the second is that the search is recognized to its controller action only it fails when i put spaces or special characters in it
when i try to add \s in the constraints of the searchkey ( '[\sa-zA-Z0-9_-]+' ) it routes to the edit function
the route of search looks like this
Edited the route to this and it worked
'search' => array(
'type' => 'literal',
'options' => array(
'route' => '/app/search',
'defaults' => array(
'controller' => 'App\Controller\App',
'action' => 'search',
),
),
'may_terminate' => true,
'child_routes' => array(
'search' => array(
'type' => 'segment',
'options' => array(
'route' => '/[:searchkey]',
'constraints' => array(
'searchkey' => '[a-zA-Z0-9_\+-]+'
),
'defaults' => array(
'action' => 'search'
),
),
),
),
),
i hope it's clear what i want if any code snippits are needed please ask
Sorted it out!
If one needs to get routes in his application this way you can use the code below
/App App/Index
/App/name-of-article App/View
/App/123 App/View
/App/name-of-article/edit App/Edit
/App/123/edit App/Edit
/App/recent App/Recent
/App/search App/Search
/App/new App/New
In my module.config.php i changed the route to:
'app' => array(
'type' => 'segment',
'options' => array(
'route' => '/app[/:action]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'App\Controller\App',
'action' => 'index',
),
),
'may_terminate' => true,
),
'search' => array(
'type' => 'literal',
'options' => array(
'route' => '/app/search',
'defaults' => array(
'controller' => 'App\Controller\App',
'action' => 'search',
),
),
'priority' => 1,
'may_terminate' => true,
'child_routes' => array(
'search' => array(
'type' => 'segment',
'options' => array(
'route' => '/[:searchkey]',
'constraints' => array(
'searchkey' => '[\sa-zA-Z0-9_\+-]+'
),
'defaults' => array(
'action' => 'search'
),
),
'priority' => 2,
'may_terminate' => true,
),
),
),
'new' => array(
'type' => 'literal',
'options' => array(
'route' => '/app/new',
'defaults' => array(
'controller' => 'App',
'action' => 'recent',
),
),
'priority' => 2,
),
'view' => array(
'type' => 'regex',
'options' => array(
'regex' => '/app/(?<view>[a-zA-Z0-9_-]+)',
'defaults' => array(
'controller' => 'App\Controller\App',
'action' => 'view',
),
'spec' => '/app/%view%',
),
),
'edit' => array(
'type' => 'regex',
'options' => array(
'regex' => '/app/(?<view>[a-zA-Z0-9_-]+)/(?<edit>[a-zA-Z0-9_-]+)',
'defaults' => array(
'controller' => 'App\Controller\App',
'action' => 'edit',
),
'spec' => '/app/%view%/%edit%',
),
),
If you need other routes you can just add a new literal route below te app route, make sure you don't forget to set the priority

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

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!