dongpao5127 2011-07-01 07:46
浏览 65
已采纳

Symfony2 - Doctrine - 2个bundle之间的实体关系

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.

  • 写回答

1条回答 默认 最新

  • duanlvxing7707 2011-07-01 07:59
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)