Doctrine2 entities in multiple namespaces - php

I'm developing a web application using Zend Framework 2 which will be made of several modules, and I'd like to put the entity classes in the module to which they belong.
Is it possible to do this using Doctrine2 ORM? By reading the docs, it seems to always expect to have all the entities under at most one namespace, while I'd like to have
Module1\Entity
Module2\Entity
and so on...
How could this be made possible?
Thanks to all!

The first step to doctrine configuration is within your global configuration file to set up the connection. Personally i do this in two files, the first is ./config/autoload/global.php and the second one being ./config/autoload/local.php
This is for one very reason and this is that anything containing local doesn't get posted into my git repositories. So my credentials are safe.
./config/autoload/global.php
return array(
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'host' => 'localhost',
'port' => '3306',
'dbname' => 'dbname'
)
)
)
),
);
./config/autoload/local.php
return array(
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'params' => array(
'user' => 'root',
'password' => ''
)
)
)
),
);
The second step would be to create a driver for your entities. This is done on Module Namespace base.
./modules/ModuleNamespace/config/module.config.php
<?php
namespace ModuleNamespace;
return array(
//... some more configuration
'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'
)
)
)
)
);
What's happening there? Well, we extend the doctrine['driver'] array by adding a new driver. The driver has the namespace of our module. For this we also need to define the namespace in our configuration file. The driver defines that all Entities for this driver are within a certain path.
The next step done is that the orm_defaults driver gets extended by an assignment defining that all ModuleNamespace\Entity classes are loaded from our ModuleNamespace_driver configuration.
And ultimately this is done for each single module. So no matter if you're having a Filemanager\Entity\File or PictureDb\Entity\File classes, both will work and both will get loaded. Modules are - by nature - independant from each other. Though they can have dependencies, or rather work well together, they function on their own. So multiple modules with multiple entities are no problem at all ;)
I hope this makes you understand the topic a little bit. For live working examples i have wrote two blog posts covering the topic.
Installing Doctrine 2 for Zend Framework 2
First Steps with Doctrine 2
These may also help you out a little bit.

If you are using the DoctrineORMModule Proxies will be written to /data/DoctrineORMModule/Proxy. I'm not sure if you have to create the folder manually and adapt privileges.
Attention:
For some reason the ZendSkeletonApplication ships without namespaces set!
ZendSkeletonApplication / module / Application / config / module.config.php
You may get this error if you forget to set the namespace in each module.config.php!
The class ... was not found in the chain configured namespaces ZfcUser\Entity, \Entity, ZfcUserDoctrineORM\Entity

Related

How to get translator base_dir from a URI in Zend Framework 2?

Helle ZF2 Guru! Normally we get the zf2 translation file from a directory in Appliction module.config.php like this: 'base_dir' => DIR . '/../language' .
Is it possible to get it from a Uri?
Application module.config.php:
'translator' => array(
'locale' => 'en_US',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',//http://example.com/
'pattern' => '%s.mo',
),
),
),
Why this is important?Correct me if I am wrong! In a multilingual application, language files are static files and for a real world application any static files better be on cloud like AWS CloudFront or CDON for better performance of Application and longer caching period.
I don't think this way is possible and I think it's not interresting to have this file outside the App directory because it's a part of your PHP software which will be interpreted and "compiled" by your web server at each HTTP request.
It's interresting for a JSON file downloaded by the navigator with AJAX request for example because the navigator will store the file in cache.
Our solution was to determine what is the current language in application.config.php:
define ('SITE_LANG', ...);
In our case it was simply the first two letters of the path:
www.site.com/en/...
Then in module.config.php:
'translator' => [
'locale' => SITE_LANG,
'translation_file_patterns' => [
[
'type' => 'phparray',
'base_dir' => 'vendor/zendframework/zendframework/resources/languages/',
'pattern' => '%s/Zend_Validate.php',
],
and it did the trick.

Doctrine Resolve_target_entity in config

I found this on the doctrine website page:
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/cookbook/resolve-target-entity-listener.html
A way to let my entity communicate with an interface which then can be configurable. The only problem is that i cant find anywhere how to put it in my array config. I already checked the configuration source but there is nothing in the docs:
https://github.com/doctrine/DoctrineORMModule/blob/master/docs/configuration.md
Hope someone can help
Thanks
You can use it like this:
'doctrine' => array(
'entity_resolver' => array(
'orm_default' => array(
'resolvers' => array(
'MyModule\Entity\FooInterface' => 'OtherModule\Entity\Foo',
),
),
),
We use it e.g. here (as a live example) in Soflomo\Blog.

Creating new Module in Zend Studio (Zend Framework 2)

I have been following the tutorial from Zend (http://www.youtube.com/watch?feature=player_embedded&v=EerB9bTvqrY) however when I add a new Module in my project I can not navigate to it, are the instructions in this tutorial incorrect?
Basically when I add a new Module in Zend Studio to my Zend Framework project I can not navigate to it, my new module is called "deals". I navigate to localhost/dealproject/deals and I get error 404. When navigating to localhost/dealproject/ it loads the zend skeleton application page correctly.
Thanks for your help.
module.config.php
<?php
return array(
'controllers' => array(
'invokables' => array(
'Deals\Controller\Deals' => 'Deals\Controller\DealsController',
),
),
'router' => array(
'routes' => array(
'deals' => array(
'type' => 'Literal',
'options' => array(
// Change this to something specific to your module
'route' => '/deals',
'defaults' => array(
// Change this value to reflect the namespace in which
// the controllers for your module are found
'__NAMESPACE__' => 'Deals\Controller',
'controller' => 'Deals',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
// This route is a sane default when developing a module;
// as you solidify the routes for your module, however,
// you may want to remove it and replace it with more
// specific routes.
'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(
),
),
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'Deals' => __DIR__ . '/../view',
),
),
);
Make sure you've enabled the module in ~/config/application.config.php
'modules' => array(
'Application',
'Deals',
),
To me the route configuration looks correct and it should show results. What I noticed is that your base URL (localhost/dealproject/) for the ZF2 application is in a sub directory of the domain's root. As you probably know all the requests are mapped to the index.php of your application (~/public/index.php). This is done by some configuration in your .htaccess file (in the same directory).
In the example URL www.example.com/blog if blog doesn't exists as a folder under www.example.com's root you get a 404 Page not found. However, the .htaccess of ZF2 makes apache call the index.php in the domain's root if no directory is found (this assumes you use the ZendSkeletonApplication).
Now, since your index.php is not in the domain root (localhost/) but in localhost/dealproject/ and your .htaccess is (I assume) in localhost/dealproject/ as well, when you call localhost/dealproject/deals apache is looking for a directory dealproject/deals since in localhost there is no .htaccess to prepare the necessary configuration.
I would advise you to make dealproject folder the root of localhost. This will enable you to call your route like this: localhost/deals and .htaccess and index.php will be processed correctly.
Hope this helps :)
Stoyan

How do I configure Dependency Injection in Zend Framework 2?

DISCLAIMER: I'm a complete noob to Zend.
I'm evaluating Zend Framework 2 at work, and trying to configure it to work with ZfTwig for templating. (See here: https://github.com/mtymek/ZfTwig)
I got through Step 3 of the config ok, but I can't figure out Step 4.
I tried placing the following in application.config, but no good.
Where am I supposed to put this?
return array(
'di' => array(
'instance' => array(
// setup other stuff...
// ...
// setup view script resolvers - very similar to configuration
// from ZendSkeletonApplication
'Zend\View\Resolver\AggregateResolver' => array(
'injections' => array(
'Zend\View\Resolver\TemplateMapResolver',
'ZfTwig\TemplatePathStack',
),
),
'Zend\View\Resolver\TemplateMapResolver' => array(
'parameters' => array(
'map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.twig',
),
),
),
'ZfTwig\TemplatePathStack' => array(
'parameters' => array(
'paths' => array(
'application' => __DIR__ . '/../view',
),
),
),
// Tell TwigRenderer how it should locate .twig files
'ZfTwig\TwigRenderer' => array(
'parameters' => array(
'resolver' => 'Zend\View\Resolver\AggregateResolver',
),
),
),
);
Google is no help... I can't find any documentation on Zend's site or anywhere telling me where this is supposed to go.
Thanks for the help!
The di configuration is from the first betas of Zend Framework 2. Zend\Di is a component still available, but internally (as with many other modules) replaced by Zend\ServiceManager.
Basically, both are able to provide dependency injection. Only for Zend\Di it can do this kind-of automatically and for Zend\ServiceManager there are other options to make dependency injection more explicit.
To give an answer to your question: ZfcTwig is now part of ZF-Commons and https://github.com/ZF-Commons/ZfcTwig is the location you have to search for now. Just for your insights, this file is an example of a factory used by the service manager. For more background of service managers in Zend Framework 2, I have written a blog post two months ago which might be interesting.

Unable to route with module in ZF2

I've created a module, a basic copy of the the albums example given in the ZF2 documentation, however, with the new module, I am not able to access it at all - I'm always given a 404 error. I'm building this on the ZF2 skeleton.
I've got three modules loaded: Application, Frontend and Security.
Both Frontend and Security are duplicates of each other, however, I have thoroughly checked and there is no reference to old code (as I literally copied the module folder and renamed/rewrote references).
The module is also loaded in application.config.php.
Any ideas on what I'm missing?
Module Config:
return array(
'controllers' => array(
'invokables' => array(
'Security\Controller\Security' => 'Security\Controller\SecurityController',
),
),
'router' => array(
'routes' => array(
'security' => array(
'type' => 'segment',
'options' => array(
'route' => '/security[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'Security\Controller\Security',
'action' => 'index',
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'security' => __DIR__ . '/../view',
),
),
);
I had the same problem while following the skeleton application tutorial (Getting started: A skeleton application). Whenever I would go to the album url in the browser (ZendSkeletonApplication/public/album in my case), I would get a 404 error page but no details on why I got the 404. It wasn't clear to me how I would be able determine why I was getting the 404 when I had double checked everything and was pretty sure I copied and configured the Album module properly. It turned out that I was missing a slash in my route (module.config.php). For example I had 'route' => 'album[/:action][/:id]' instead of 'route' => '/album[/:action][/:id]'.
I was only able to figure it out by intentionally causing errors by misspelling things like making the 'Album\Controller\Albums' instead of 'Album\Controller\Album'in the invokables value, this would cause a stack trace to display which then showed the ZF2 classes that where called on the request. I would continue to misspell, test, and then correct each part of the module.config.php until I was given a clue to what part of the configuration was causing the error.
I'm pretty sure this was not the best way to debug an application's configuration.
There is few things that need to be make sure is:-
You have to add your module in
application.config.php (which you are saying you done it.)
Security\Controller\Security has to be same in default too (which you already has)
One more thing is Your folder structure....
-
Just to doulbe check you have a /MODULE/src/MODULE/Controller/CONTROLLER_FILE_NAME.php
I hope that helps..
I know it is an old post. However another thing to make sure you have in the modules top directory (same directory as the Module.php file) is the "autoload_classmap.php"
file with "<?php return array();?>" inside of it.
A simple tip to know whether your rule has already added correctly to the routes or not, you may check the routes value in the config file inside any working module, as following:
$config = $this->serviceLocator->get('config');
var_dump($config);

Categories