Reputation: 1748
I'm trying to understand Zend Framework 2. For that, I started with Ron Allen's tutorial http://akrabat.com/getting-started-with-zend-framework-2/ then, I integrate doctrine 2, using the tutorial http://www.jasongrimes.org/2012/01/using-doctrine-2-in-zend-framework-2/ ok, before that I decide to make it more complex.
I change the database to the following:
--
-- Estrutura da tabela `album`
--
CREATE TABLE IF NOT EXISTS `album` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`artist_id` int(11) NOT NULL,
`title` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
KEY `artist` (`artist_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=16 ;
--
-- Estrutura da tabela `artist`
--
CREATE TABLE IF NOT EXISTS `artist` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
My application have this structure:
module
Album
src
Album
Controller
AlbumController.php
Entity
Album.php
Artist
src
Artist
Controller
ArtistController.php
Entity
Artist.php
My new Entities are like that:
class Album {
/**
* @ORM\Id
* @ORM\Column(type="integer");
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string")
*/
protected $title;
/**
* @ORM\ManyToOne(targetEntity="Artist", inversedBy="album")
* @ORM\JoinColumn(name="artist_id", referencedColumnName="id")
*/
protected $artist;
...
}
class Artist {
/**
* @ORM\Id
* @ORM\Column(type="integer");
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string")
*/
protected $name;
/**
* @ORM\OneToMany(targetEntity="Album", mappedBy="artist")
*/
protected $album;
public function __construct()
{
$this->album = new ArrayCollection();
}
...
}
But It doesn't work! I got this mensage:
"The target-entity Album\Entity\Artist cannot be found in 'Album\Entity\Album#artist'."
So my question is: What is wrong? My Entities are in the wrong place? or my module organization are not ok? How can I make one entity be visible for more than one module?
UPDATE:
I change my entities to:
class Album {
/**
* @ORM\Id
* @ORM\Column(type="integer");
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string")
*/
protected $title;
/**
* @ORM\ManyToOne(targetEntity="\Artist\Entity\Artist", inversedBy="album")
* @ORM\JoinColumn(name="artist_id", referencedColumnName="id")
*/
protected $artist;
...
}
class Artist {
/**
* @ORM\Id
* @ORM\Column(type="integer");
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string")
*/
protected $name;
/**
* @ORM\OneToMany(targetEntity="\Album\Entity\Album", mappedBy="artist")
*/
protected $album;
...
}
But I got the same error:
"The target-entity Artist\Entity\Artist cannot be found in 'Album\Entity\Album#artist'."
UPDATE 2:
I changed the structure of my application to:
module
Album
src
Album
Controller
AlbumController.php
ArtistController.php
Entity
Album.php
Artist.php
so my entities are in the same namespace and now my program it's working! =)
But I still have the question: how can I make one entity to be visible to more than one mudule in ZF2?
Upvotes: 3
Views: 6383
Reputation: 1748
I found the answer! =D
I got to wait 8 hours to answer my own question, so here we go.
As I said, I reproducing the tutorial http://www.jasongrimes.org/2012/01/using-doctrine-2-in-zend-framework-2/
They teach how to configure the module to work with Doctrine 2. In the file module/Album/config/module.config.php they insert the following code:
return array(
'di' => array(
'instance' => array(
// ...
'orm_driver_chain' => array(
'parameters' => array(
'drivers' => array(
'Album' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'namespace' => __NAMESPACE__ . '\Entity',
'paths' => array(
__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity'
),
),
),
),
),
According to the tutorial:
"This tells Doctrine that the Album module’s entities use the namespace Album\Entity, and that the classes in that namespace are stored in $PROJECT_DIR/module/Album/src/Album/Entity."
so, there is the problem! Doctrine was configure to use only Album\Entity! so I changed the code to the following (with bad programming... sorry):
//...
'drivers' => array(
'Album' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'namespace' => __NAMESPACE__ . '\Entity',
'paths' => array(
__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity'
),
),
'Artist' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'namespace' => '\Artist\Entity',
'paths' => array(
__DIR__ . '/../../Artist/src/Artist/Entity'
),
),
//...
As you can see I configure an 'Artist' driver...
Now my application work properly! =)
I'm still looking for the right way to configure the doctrine in my application but at least I got the answer!
Thanks for everybody! :)
Upvotes: 2
Reputation: 3611
Regarding your updated question:
Your Entities and all other code used in one module is "visible" within each other module, just instantiate it via
$artist = new \Album\Entity\Artist();
or whatever you may need. Just make sure to have all your modules registered in your application configuration.
Upvotes: 1
Reputation: 48865
By default Doctrine 2 will look in the same namespace as the current entity for your related entities. Something like:
* @ORM\ManyToOne(targetEntity="\Artist\Entity\Artist", inversedBy="album")
Will be needed. Disclaimer: I have not used ZF2. I'm assuming your class loader paths are all setup.
Upvotes: 0