【问题标题】:Error with Relational Table in Doctrine教义中关系表的错误
【发布时间】:2013-12-11 16:51:02
【问题描述】:

我正在尝试按照 this link 中的示例与 Doctrine 2 和 Zend Framework 1 的额外字段建立多对多关系。我的代码如下所示:

用户实体

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--

}

渠道实体

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--
}

关系 UserChannel 实体:

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--
}

现在,在我的控制器中,我有以下内容:

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

        $userChannel = $user->getUserChannel();

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

我收到两个错误:

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**

但我仍然得到正确的频道 ID(带有星号的 ID)。

我已经尝试了很多事情,但我仍然无法通过获取用户来获取与该用户相关的频道,也无法通过获取频道来获取与该频道相关的用户。我是否正确使用了关系?非常感谢您的帮助!谢谢!

【问题讨论】:

    标签: php zend-framework symfony doctrine-orm doctrine


    【解决方案1】:

    当我开始使用 Doctrine 时,我自己也犯了同样的错误。

    您不能混合使用 ColumnJoinColumn 注释。尝试从userUserChannel 中的channel 属性中删除Column 注释...

    【讨论】:

    • 让我告诉你:你摇滚!
    • 如果您使用 Doctrine 的默认外键命名方案(即属性 + '_id'),我更愿意删除 @JoinColumn 注释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-08
    • 2011-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多