简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM