dongxie2613 2013-12-11 16:51
浏览 36
已采纳

Doctrine中关系表的错误

I'm trying to follow the example from this link to do a many to many relationship with an extra field with Doctrine 2 and Zend Framework 1. My code looks like this:

User Entity

class User
{
    /**
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    private $user_id;

    /**
     * @OneToMany(targetEntity="UserChannel", mappedBy="user")
     */
    private $userChannel;

    /**
     * Entity constructor
     */
    public function __construct()
    {
        $this->userChannel = new ArrayCollection(); 
    }

   --GETTERS AND SETTERS--

}

Channel Entity

class Channel
{
    /**
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    private $channel_id;

    /**
     * @OneToMany(targetEntity="Userchannel", mappedBy="channel")
     */
    private $userChannel;


    /**
     * Entity constructor
     */
    public function __construct()
    {
        $this->userChannel = new ArrayCollection();     
    }

    --GETTERS AND SETTERS--
}

Relational UserChannel Entity:

class UserChannel
{
    /**
     * @Id @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    private $id;

    /** @Column(name="channel_id", type="integer", length=11)
     *  @ManyToOne(targetEntity="Channel", inversedBy="userChannel")
     *  @JoinColumn(name="channel_id", referencedColumnName="channel_id")
     */
    private $channel;   

    /** @Column(name="user_id", type="integer", length=11)  
     *  @ManyToOne(targetEntity="User", inversedBy="userChannel")
     *  @JoinColumn(name="user_id", referencedColumnName="user_id")
     */
    private $user;

     /**
     * Entity constructor
     */
    public function __construct()
    {
    }

    --GETTERS AND SETTERS--
}

Now, in my controller I have the following:

           $user = $this->em->getRepository('Entities\User')->find(1);

        $userChannel = $user->getUserChannel();

        foreach($userChannel as $channel){
            print_r($channel->getChannel());
        }

I get two errors:

Notice: Undefined index: user in /Doctrine/ORM/Persisters/BasicEntityPersister.php on line 1396

Warning: Invalid argument supplied for foreach() in /Doctrine/ORM/Persisters/BasicEntityPersister.php on line 1401
**205**

But I still get the right channel id (the id in with the stars).

I have tried many things, but I still can't, by getting a User, get the Channel related to that User nor by getting the Channel, get the Users related to that channel. Am I using the relations correctly? Would really appreciate the help! Thanks!

  • 写回答

1条回答 默认 最新

  • duanfangfei5558 2013-12-11 20:59
    关注

    Made same mistake myself when I was starting off with Doctrine.

    You cannot mix Column and JoinColumn annotations. Try removing Column annotation from user and channel properties within UserChannel...

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

报告相同问题?

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序