How to configure Doctrine file mapping driver in ZF2 - php

I have this error:
Fatal error: Uncaught exception
'Doctrine\Common\Persistence\Mapping\MappingException' with message
'File mapping drivers must have a valid directory path, however the
given path [path/to/my/entities] seems to be incorrect
and i have this in my module.config.php:
'doctrine' => array(
'driver' => array(
// defines an annotation driver with two paths, and names it `my_annotation_driver`
'my_annotation_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(
__DIR__ . '/../src/Realez/Entity',
'another/path'
),
),
// default metadata driver, aggregates all other drivers into a single one.
// Override `orm_default` only if you know what you're doing
'orm_default' => array(
'drivers' => array(
// register `my_annotation_driver` for any entity under namespace `My\Namespace`
'Realez/Entity' => 'my_annotation_driver'
)
)
)
)

I had exactly the same problem. I solved it by creating an empty Entity directory in the location where doctrine expect me to store my entities. All you have to do is create in following location an empty Entity directory: __DIR__ . '/../src/Realez/Entity'.

Modify your module.config.php file.
return array(
'doctrine' => array(
'driver' => array(
__NAMESPACE__.'_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/'.__NAMESPACE__.'/Entity')
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__'\Entity' => __NAMESPACE__.'_driver'
),
),
),
),
);

**File mapping drivers must have a valid directory path, however the given path [path/to/my/entities]**
this mean you don't have an Entity folder at that directory
You just need to create one at that location

Ensure your paths are correct.
__NAMESPACE__ . '_driver' => [
'class' => AnnotationDriver::class,
'cache' => 'array',
'paths' => [__DIR__ . '/../src/Entity/'],
// or: 'paths' => [__DIR__ . '/../src/Entity/'.__NAMESPACE__.'/Entity']
],
'orm_default' => [
'drivers' => [
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
]
],
Also you may try clearing metadata cache with doctrine command line tool:
./doctrine-module orm:clear-cache:metadata

Related

Doctrine error in mapping many namespaces inside Zend Framework 2 module structure

I'm getting an error from Doctrine2 while it tries to map my class structure.
My Zend Application's modules is structured as follows:
module
ModuleOne
config
module_config.php
src
ModuleOne
Entity
ClassOne.php
ModuleOneAdmin
Entity
ClassTwo.php
ClassTwoRepository.php
Module.php
ModuleTwo
config
module_config.php
src
ModuleTwo
Entity
ClassThree.php
Module.php
I've two subnamespaces inside ModuleOne, so, my autoloader (in ModuleOne/Module.php) is configured this way:
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ . 'Admin' => __DIR__ . '/src/' . __NAMESPACE__ . 'Admin',
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__
),
),
);
}
In ModuleOne/config/module_config.php, the doctrine is configured
'doctrine' => array(
'driver' => array(
__NAMESPACE__ . '_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(
__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity'
)
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
),
),
),
)
I'm using the ModuleOne and ModuleTwo in another module (that I didn't specified in modules structure to avoid making it exhaustive) that possesses Controllers and Views.
When I run my application, it gets and error in a line that calls for a repository of the an Entity:
$repository = $this->getEm()->getRepository('ModuleOneAdmin\Entity\ClassTwo');
The error is:
The class 'ModuleOneAdmin\Entity\ClassTwo' was not found in the chain configured namespaces ModuleOne\Entity, ModuleTwo\Entity
I've searched a lot of other questions about something like this here in StackOverflow and in others sites from a google search, but none addressed my issue. Its like the Doctrine can't find my ModuleOneAdmin in its internal mappings. Is there some configuration that I'm missing?
Thanks in advance.
I've found the problem, I didn't included the path to the drivers of the admin entities folder, in the doctrine configurations.
With the code below it works.
'doctrine' => array(
'driver' => array(
__NAMESPACE__ . '_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(
__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity'
)
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver',
__NAMESPACE__ . 'Admin\Entity' => __NAMESPACE__ . '_driver'
),
),
),
)

How to store queries and their results in memcache using cache enabled for doctrine 2 (using zend framework 2 )?

I guess all is in the title , I've looked a lot for a solution but can't figure out how to do it ? Here is my configuration for memcache in module.config.php :
// Doctrine config
'doctrine' => array(
'driver' => array(
__NAMESPACE__ . '_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
),
)
),
/***** enabling the memcache ****/
'configuration' => array(
'orm_default' => array(
'metadata_cache' => 'mycache',
'query_cache' => 'mycache',
'result_cache' => 'mycache',
)
/**** end ****/
)
),
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
'doctrine.cache.mycache' => function ($sm) {
$cache = new \Doctrine\Common\Cache\MemcacheCache();
$memcache = new \Memcache();
$memcache->connect('localhost', 11211);
$cache->setMemcache($memcache);
return $cache;
},
),
),
i'm using zend framework 2 and doctrine 2 .
thanks.
If everything is configured correctly you don't need to configure anything else for Query Cache and Metadata Cache to work, however, to enable Result Cache you will have to call useResultCache explicitly on each query.
Example:
<?php
$query = $em->createQuery('select u from \Entities\User u');
$query->useResultCache(true);

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

ZF2: Module could not be initialized

I'm trying to get started with ZF2 and I have a problem when I writting code from tutorial (on ZF website). My code:
Module.php:
<?php
namespace About;
class About
{
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php',
),
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
}
?>
config/module.config.php:
<?php
return array(
'controllers' => array(
'invokables' => array(
'About\Controller\About' => 'About\Controller\AboutController',
),
),
'router' => array(
'routes' => array(
'album' => array(
'type' => 'segment',
'options' => array(
'route' => '/about[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'About\Controller\About',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'about' => __DIR__ . '/../view',
)
),
);
Problem is:
Fatal error: Uncaught exception 'Zend\ModuleManager\Exception\RuntimeException' with message 'Module (About) could not be initialized.' in /var/www/zend2/vendor/zendframework/zendframework/library/Zend/ModuleManager/ModuleManager.php on line 175
Why it's shown on start? (in my project: /var/www/zend2/). If I remove module declaration from application.config.php it works okay. What is my problem? :/
Ouch, solved!
In Module.php class must be named Module, not own name...
When having this issue with PSR-4 loading you could also check whether the module name and path to the folder are correct for autoloading inside your composer.json file:
"autoload": {
"psr-4": {
"YourModule\\": "module/YourModule/src/",
... other modules ...
}
},
And then after fixing things run:
composer update
I believe your class indexes are outdated, you can follow this command:
composer install -o

Zend Framework 2 How To Set Doctrine 2 Proxy Directory

So I am using Doctrine 2 module in Zend Framework 2 configured according to Jason Grimes' turorial (http://www.jasongrimes.org/2012/01/using-doctrine-2-in-zend-framework-2/).
Sometimes I keep getting this error though:
Your proxy directory must be writable.
How can I set the proxy directory?
Here is my Doctrine configuration from module.config.php:
'doctrine' => array(
'driver' => array(
__NAMESPACE__ . '_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
),
),
),
),
The default proxy directory is data/DoctrineORMModule/Proxy. I guess you know how to make it writable, right?
If you need to change it for some reason, you can overwrite the appropriate configuration key:
<?php
return array(
'doctrine' => array(
'configuration' => array(
'<YOUR DRIVER NAME (orm_default by default)>' => array(
'proxy_dir' => 'data/DoctrineORMModule/Proxy',
'proxy_namespace' => 'DoctrineORMModule\Proxy',
)
)
)
);
?>
Hope this helps.

Categories