Doctrine 2 Autloading - zend-framework

How do set up autoloading with Doctrine 2 and Zend to load entities in the following directory structure:
Application
-Modules
--Core
---Models
----Entities
----Repositories
--CMS
---Models
----Entities
----Repositories
I want to be able to load classes using {ModuleName}\Entities{EntityName}. For example, I'd like to be able to do this to load a 'User' entity:
$em->getRepository('Core\Entities\User');
or something like this for a 'Pages' entity:
$em->getRepository('CMS\Entities\Pages');
I can set it up to load 'CMS\Models\Entities\Pages' but I'd like to be able to know how to do it without having to map directly to the directory structure. Is this possible?

I don't how you glue Zend Framework and Doctrine2 together but if you are using the popular Bisna glue (which is pretty cool) you can set-up more than one mapping directory in your application.ini. Take a closer look to the following ini settings:
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.adapterClass = "Doctrine\ORM\Mapping\Driver\AnnotationDriver"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.mappingNamespace = "Core\Entities"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.mappingDirs[] = APPLICATION_PATH "/modules/Core/Entities"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.annotationReaderClass = "Doctrine\Common\Annotations\AnnotationReader"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.0.annotationReaderCache = default
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.1.adapterClass = "Doctrine\ORM\Mapping\Driver\AnnotationDriver"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.1.mappingNamespace = "CMS\Entities"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.1.mappingDirs[] = APPLICATION_PATH "/modules/CMS/Entities"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.1.annotationReaderClass = "Doctrine\Common\Annotations\AnnotationReader"
resources.doctrine.orm.entityManagers.default.metadataDrivers.drivers.1.annotationReaderCache = default
Something like the above would be accomplish what you want. If want to be able to do this fully automatically I think you have to patch the Bisna\Doctrine\Container class. Which for instance looks to the modules defined check if there is a entities directory and add's this to the doctrine entity manager.
Bisna
If you don't have a clue what Bisna is, this is a small library which allows you to easily 'glue' Doctrine2 and Zend Framework 1 together.
By watching this video it should be easy for you to understand how to integrate Doctrine2.
http://www.zendcasts.com/unit-testing-doctrine-2-entities/2011/02/
Please be aware that the Bisna version used in the video only supports Doctrine 2.0 and not 2.1 in that case you should use this one: https://github.com/guilhermeblanco/ZendFramework1-Doctrine2

Related

Iterating through entity framework models from another related project

I have just started working with .Net and I have created an Entity Framework Model and an associated context for the password reset functionality of a website and I have created this in a class library called MYSITE.Reset.Data with 3 classes (email,mapping,link).
I have now created a windows form application MYSITE.Reset but am having trouble in iterating through my models from the program.cs file. I am not quite sure of the structure of the syntax and I have unsuccessfully tried the following:
foreach(MYSITE.Reset.Data.Maps mp)
Try this:
var maps = from m in context.Maps
select m;
foreach(var map in maps)
{
// do stuff
}

Laravel 4 using vendor classes

I have installed Laravel 4 after using 3, love it.
I used to be able to use the Zend framework as such:
$yt = new Zend_Gdata_YouTube();
for instance
I have used composer to install Zend and everything is installed in the Vendor folder..
Problem:
How to address the individual classes i.e. Zend Gdata etc.
I can't find any documentation on calling classes from a vendor in L4.
Any help is appreciated.
Take a look at your vendor\composer\autoload_classmap.php file. In there you will find a list of all vendor classes that are being autoloaded. I think all classes will have to be called using their full namespaced name.
E.g.
I'm using Zizaco's Entrust package. This is what it looks like in the vendor\composer\autoload_classmap.php file.
'Zizaco\\Entrust\\Entrust' => $vendorDir . /zizaco/entrust/src/Zizaco/Entrust/Entrust.php',
If I wanted to access the Entrust.php class I have to call
$en = new Zizaco\Entrust\Entrust();
Alternatively you could alias certain classes in your app\config\app.php file.
E.g.
'Ent' => 'Zizaco\Entrust\Entrust'
In your case you'll need to do something like this:
$yt = new Zend\namespace\Zend_Gdata_YouTube();

Setting-up Application.ini in Zend Framework for Model

I'm new to Zend Framework. I've been following book "ZendFramework - A Beginners Guide". in the 4th chapter, they start using Doctrine. The thing is that I'd like to use Zend's built-in functionality with database interactions.
I've searched the web and watched couple of tuts. Well I couldn't find the right (for me) solution.
in all tuts, everyone is using standard Zend's structure. I'd like to integrate my Modules. So I have a file structure like this:
application
/modules
/moduleName
/controllers
/models
/views
I have set up the routes in application.ini for controllers and views like this:
; /articles/* route
resources.router.routes.articles.route = /articles
resources.router.routes.articles.defaults.module = content
resources.router.routes.articles.defaults.controller = articles
resources.router.routes.articles.defaults.action = index
Now I tried to load-up my models so I start interacting with the tables in Mysql.
in my controller named ArticlesController.php I created the class Content_ArticlesController and in there I have:
public function indexAction()
{
$model = new Content_ArticlesModel();
$this->view->modelHi = $model->there;
$this->view->hello = 'Hello there';
}
As you might guess Content_ArticlesModel is my class that extends Zend_Db_Table_Abstract class. in there I have a public function modelHi() that I'd like to call from my Controller to pass the data from the model to the view.
Can someone help me do this in the right way? how should I correctly load-up my models for my modules? and maybe you could link some good article on working with database based on some example.
EDIT: Ok, I had this really studip error in my Controller but it's ok. Anyways the problem is that when Zend loads-up my controller it displays: Fatal error: Class 'Content_ArticlesModel' not found in S:\xampp\htdocs\testsite\application\modules\content\controllers\ArticlesController.php on line 12
The issue is my Zend can't locate/doesn't know where to find the models of my module that I'm trying to reach from my controller.
I want to know how should I make Zend see and use my models?
You should make configuration in application.ini file for modeule and view
resources.modules =
resources.view[] =
Also you should make bootstrap file inside your module
class Content_Bootstrap extends Zend_Application_Module_Bootstrap {
}
Also you should ensure that the name of your modulel folder is small letter "content"..
After making these configuration, you will be able to call model inside controller...
Why not just call your public modelHi() method in your controller
Update:
If your module name is 'content' , then your model should be located
application/modules/content/models/ArticlesModel.php
then do the following
public function indexAction()
{
$model = new Content_Model_ArticlesModel(); //name of class
$this->view->modelHi = $model->modelHi();
$this->view->hello = 'Hello there';
}

Integrate Doctrine Extensions into my ZF / Doctrine 2 project

I want to do this to be able to take advantage of Gedmo translatable to translate values in my tables. I already have Doctrine working in my project, but I don't know how to add these extensions : doctrine extensions
I am using ZF Boilerplate and apparently it should be fairly simple, but I am lost. Any ideas?
Add namespace:
autoloaderNamespaces.app[] = "Gedmo"
Add event subscriber:
resources.doctrine.dbal.connections.default.eventSubscribers[] = "Gedmo\Translatable\TranslatableListener"
Add annotation driver:
resources.doctrine.orm.entityManagers.default.metadataDrivers.annotationRegistry.annotationNamespaces.0.namespace = "Gedmo"
resources.doctrine.orm.entityManagers.default.metadataDrivers.annotationRegistry.annotationNamespaces.0.includePath = APPLICATION_PATH "/../library"
I realize this was posted some time ago, and this might be a shot in the dark, but adding these two lines to my application.ini config did the trick for me:
resources.doctrine.orm.entityManagers.default.metadataDrivers.annotationRegistry.annotationNamespaces.0.namespace = "Gedmo"
resources.doctrine.orm.entityManagers.default.metadataDrivers.annotationRegistry.annotationNamespaces.0.includePath = APPLICATION_PATH "/../library/vendors"
Hope this helps someone!

zend + doctrine 2 doctrine manager, where is it?

i see everyone using this:
Doctrine_Manager::getInstance()
when i do this, its error is:
Class 'Doctrine_Manager' not found
how do i load this ?so that i can start get instances from doctrine manager?
i want to load this:
$con = Doctrine_Manager::getInstance()->connection();
$st = $con->execute("...............");
$result = $st->fetchAll();
where can autoload this , so i can call the getInstance() function from anywhere?
thanks...
Doctrine_Manager is part of version 1.2, not 2. If you are actually using 1.2, you need to let the autoloader know to load classes under the Doctrine_ prefix.
To do so, add this to your application configuration file...
autoloaderNamespaces.Doctrine = "Doctrine_"
You also need to ensure the doctrine classes can be found on the include path. If they aren't in your "library" folder or otherwise part of the include_path directive, add this...
includePaths.Doctrine = "/path/to/Doctrine-1.2/lib"
I think you might be looking for the EntityManager?
If so, here you can find a tutorial how to configure.
Also there is a library call Bisna for integrating ZF+Doctrine2, here is a good tutorial video for configuring it