As a New Year’s Day hackathon thought I would take Rob Allen’s great zend framework 2 beta tutorial and substitute doctrine2 instead of Zend\Db\Table using the ZF2 modules SpiffyDoctrine and SpiffyDoctrineORM.
Everything was going pretty well, got the entity manager going and set up my entity:
<?php
namespace AlbumDoc\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="album")
*/
class Album {
/**
* @ORM\Id
* @ORM\Column(type="integer");
* @ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @ORM\Column(type="string")
*/
public $artist;
/**
* @ORM\Column(type="string")
*/
public $title;
/**
* global getter
*
* @param type $property
* @return type
*/
public function __get($property) {
return $this->$property;
}
/**
* Global setter
*
* @param type $property
* @param type $value
*/
public function __set($property, $value) {
$this->$property = $value;
}
}
As I’m new to Doctrine and Zend Framework for that mater, I thought I would do a test to see if I could make the Entity Manager save to the database. I setup my code in the indexController as follows:
$em = $this->getLocator()->get('doctrine_em');
$album = new \Application\Entity\Album();
$album->artist = 'Art Ist';
$album->title = 'Cool Title';
$em->persist($album);
$em->flush();
The problem I’m having it that when this code runs I get the following error:
Class Application\Entity\Album is not a valid entity or mapped super class.
From the limited stuff I have found I think the problem has something to do with Doctrine not knowing the entity path or something to do with the AnnotationDriver.
Guessing there is something that needs to be added to the Album module’s config file but can’t find what.
Updated: Since I don’t have enough reputation points to post the answer formally I’ll add the answer here.
Found the solution. There were actually two parts to what was going wrong.
First was a stupid mistake, I forgot to take the .dist off the end of the module.spiffy_doctrine_orm.config.php file that gets dropped into the applications config/autoload directory.
Second part was in this file, I didn’t alter the driver settings of the settings array to point to:
'driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'namespace' => 'AlbumDoc\Entity',
'paths' => array('module/AlbumDoc/src/AlbumDoc/Entity')
)
This does beg the question of whether it would be possible for each module to hold it’s own entities, if you are setting a global entity path for your application. But that can wait for another day.
(Anwering the second question)
Yes this is possible and (in my oppinion) the way to go. Your module’s config just has to return a subset of the orm-configuration you want to change/extend.
Unfortunatelly Doctrine’s drivers don’t seem to support multiple namespaces. Therefore you’ll have to add a new driver for each namespace (not sure about this, correct me if i’m wrong 🙂
To add a new driver, let you module’s config contain something like:
This will add a new driver to the configuration without touching any non-module related config-files.