Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 - Doctrine - Entity relationship between 2 bundles

I can't figure out how i can have an entity relationship between 2 bundles.

The closest i've been to succeed so far is this:

User Entity:

<?php
namespace Acme\AuthBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

use Symfony\Component\Validator\Constraints as Assert;

use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\generatedValue(strategy="AUTO")
     */
    protected $id;

    //...

    /**
     * @ORM\OneToMany(targetEntity="Acme\NewsBundle\Entity\Article", mappedBy="author")
     */
    protected $articles;


    //...

Article Entity:

<?php
namespace Acme\NewsBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="Acme\NewsBundle\Entity\ArticleRepository")
 * @ORM\Table(name="articles")
 */
class Article
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    //...

    /**
     * @ORM\ManyToOne(targetEntity="Acme\AuthBundle\Entity\User", inversedBy="articles")
     */
     protected $author;

     //...

When generating entities and updating the database, i get no errors and the relation is correctly set in the DB.

But when I try to fetch the articles i get this:

Class Acme\AuthBundle\Entity\Article does not exist
500 Internal Server Error - ReflectionException 

Note that the User entity is in AuthBundle and Article entity in NewsBundle.

Thanks.

like image 849
LBridge Avatar asked Jul 01 '11 07:07

LBridge


1 Answers

You need to specify the name of entity class when defining one-to-many, many-to-one relations. Still you are specifying Acme\AuthBundle\Entity\User not Aief\AuthBundle\Entity\User as targetEntity.

The same for Article and Article repository.

like image 104
Gedrox Avatar answered Sep 16 '22 14:09

Gedrox