Zend Framework 2 How To Set Doctrine 2 Proxy Directory - php

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.

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

How to configure Doctrine file mapping driver in ZF2

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

Multiple namespaces under same module in ZF2

I'm having trouble configuring multiple namespaces/classes under same module.
For example, I have a module called "Account", in which I'd like to include all account related classes (companies: 'accounts', users: 'users', external api: 'api' etc.. ). Module structure looks like this..
/Account
- Module.php
- /config
- /view
- /src
- /Account
- /Controller (AccountController.php)
- /Form (AccountForm.php)
- /Model (Account.php + AccountTable.php)
- /User
- /Controller (UserController.php)
- /Form (UserForm.php)
- /Model (User.php + UserTable.php)
- /Api
- Api.php (simple class)
Being new to ZF2, I decided to keep things simple and stupid and Not to try implementing complex routing to Account module. So, in order to trigger indexAction for UserController, url should be /user (!)
Here's the module class:
namespace Account;
use Account\Model\AccountTable;
use Account\Model\UserTable;
class Module
{
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 getServiceConfig()
{
return array(
'factories' => array(
'Account\Model\AccountTable' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$table = new AccountTable($dbAdapter);
return $table;
},
'Account\Model\UserTable' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$table = new UserTable($dbAdapter);
return $table;
},
),
);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
}
And the module.config file
return array(
'controllers' => array(
'invokables' => array(
'Account\Controller\Account' => 'Account\Controller\AccountController',
'Account\Controller\User' => 'Account\Controller\UserController',
),
),
'router' => array(
'routes' => array(
'account' => array(
'type' => 'segment',
'options' => array(
'route' => '/account[/:action[/:accountId]]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'accountId' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Account\Controller\Account',
'action' => 'index',
),
),
/*
'may_terminate' => true,
'child_routes' => array(
'user' => array(
'type' => 'literal',
'options' => array(
'route' => '/user[/:action[/:userId]]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'userId' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Account\Controller\User',
'action' => 'index'
)
)
)
),
*/
),
'user' => array(
'type' => 'segment',
'options' => array(
'route' => '/user[/:action[/:userId]]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'userId' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Account\Controller\User',
'action' => 'index',
),
),
)
),
),
'view_manager' => array(
'template_path_stack' => array(
'account' => __DIR__ . '/../view',
'user' => __DIR__ . '/../view',
),
),
);
But the error I'm getting is, "Class 'Account\Controller\UserController' not found". I am sure i've missed something. Any clue please?
Thanks
You must let the StandardAutoloader know about your new namespace:
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php',
),
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
// This is for the Account namespace
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
// And this is for the User namespace
'User' => __DIR__ . '/src/' . 'User',
),
),
);
}
In the module.config.php
return array(
'controllers' => array(
'invokables' => array(
'Account\Controller\Account' => 'Account\Controller\AccountController',
// The key can be what ever you want, but the value must be a valid
// class name. Your UserController lives in the User namespace,
// not in Account
'Account\Controller\User' => 'User\Controller\UserController',
),
),
/* ... */
);
The StandardLoader needs to know where to find the classes. You can define it with an option called namespaces which is an array that contains absolute (or relative to the current script) paths. It should look like this:
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__
)
)
__NAMESPACE__ is the name of the module, and __DIR__ the absolute path to the Module.php script
Check http://framework.zend.com/manual/2.0/en/modules/zend.loader.standard-autoloader.html
The ClassMapAutoloader is used for performance: you define the class key and its exactly path to the file, instead of a folder which zf2 has to browse its contents doing filesystem operations (StandardLoader's way).
Check http://framework.zend.com/manual/2.0/en/modules/zend.loader.class-map-autoloader.html

ZF2 Module based Layouts

I'm trying to learn Zend Framework 2 and currently facing an issue.
I have created a module named "Admin" and have defined layout for Admin Module. Now problem is Application module is also loading Admin Module's layout. If I browse Admin or Application module, same layout is being loaded. I have tried multiple solutions by Googling but didn't get anyone working.
I have created Admin module by copying Application module dir and renaming it to Admin and changed "Application" to "Admin" in sub directories name and code files.
This is the visual presentation of Khalids post with one more addition in the config/application.config.php file
Step 1.
Step 2.
Copy Application Module to and rename to Admin
//config/application.config.php
'modules' => array(
'Application'
,'Test', // add this line
),
<?php
//Step 3.
//your Test/config/module.config.php should look like as follows
return array(
'router' => array(
'routes' => array(
/* 'home' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => '/',
'defaults' => array(
// 'controller' => 'StickyNotes\Controller\Album',
//'controller' => 'Application\Controller\Index',
'action' => 'index',
),
),
), */
// The following is a route to simplify getting started creating
// new controllers and actions without needing to create a new
// module. Simply drop new controllers in, and you can access them
// using the path /application/:controller/:action
'application' => array(
'type' => 'Literal',
'options' => array(
'route' => '/test',
'defaults' => array(
'__NAMESPACE__' => 'Test\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
),
'service_manager' => array(
'factories' => array(
'translator' => 'Zend\I18n\Translator\TranslatorServiceFactory',
),
),
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
'controllers' => array(
'invokables' => array(
'Test\Controller\Index' => 'Test\Controller\IndexController',
),
),
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/test' => __DIR__ . '/../view/layout/test.phtml',
'application/index/index' => __DIR__ . '/../view/test/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
);
Step 4.
and finally your Test/Module file should look as follows:->
namespace Test;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
class Module
{
public function onBootstrap(MvcEvent $e)
{
$e->getApplication()->getServiceManager()->get('translator');
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
}
and you will be ready to roll.
Well, at last I have found a solution.
Suppose you want to create a module named "Admin", here are the steps:
1- Copy Application Module Dir and rename it to "Admin". It is the name of your module.
2- Update all references in Admin Module that are initially pointing towards Application module. ( change "Application" to "Application" and "application" to "admin" )
3- In Admin/config/module.config.php remove "home" route.
4- Update your layout and views.
5- Test in your browser by using http://example.com/admin
That's it.
You don't need any external layout lib like "EdpModuleLayouts"
Cheers :)

Categories