学说2关系错误

时间:2015-02-23 08:06:42

标签: php mysql symfony doctrine-orm

我在将记录插入数据库时​​遇到问题。 我有3个名为的表 通知: id,id_event,event_id,is_readed 评论: id,comment,receiver,sender,created_at 消息 id,subject,message,receiver,sender,created_at

我想将notifications.id_event的评论和消息ID与多对多关系联系起来,看起来像

oneToOne:
    comment:
        targetEntity: Comments
        joinColumn:
            name: id_event
            referencedColumnName: id

    message:
        targetEntity: Messages
        joinColumn:
            name: id_event
            referencedColumnName: id

当我尝试插入记录时,我有以下异常:

  

找到Doctrine \ Common \ Collections \ ArrayCollection类型的实体   协会Privatmarket \ BusinessBundle \ Entity \ Notifications#message,   但期待Privatmarket \ BusinessBundle \ Entity \ Messages

并且真的不知道该怎么做。也许我在选择中犯了错误?

插入代码为:

        $comment = new Comments();
        $comment->setSender(rand(1, 10));
        $comment->setReceiver(rand(11, 20));
        $comment->setComment("Lorem ipsum dolor sit amet {$i}, olor sit amet {$i}, psum dolor sit amet {$i}, olor sit amet {$i}, ");
        $em->persist($comment);
        $em->flush();

        $notification = new Notifications();
        $id = $comment->getId();
        $notification->setComment($em->getRepository("PrivatmarketBusinessBundle:Comments")->find($id));
        $notification->setEventId(2);
        $em->persist($notification);
        $em->flush();


        $message = new Messages();
        $message->setSender(rand(1, 10));
        $message->setReceiver(rand(11, 20));
        $message->setSubject("Lorem ipsu{$i}.");
        $message->setMessage("Lorem ipsum dolor sit amet {$i}, olor sit amet {$i}, psum dolor sit amet {$i}, olor sit amet {$i}, Lorem ipsum dolor sit amet {$i}, olor sit amet {$i}, psum dolor sit amet {$i}, olor sit amet {$i}, ");
        $em->persist($message);
        $em->flush();

        $notification = new Notifications();
        $id = $message->getId();
        $notification->setComment($em->getRepository("PrivatmarketBusinessBundle:Messages")->find($id));
        $notification->setEventId(1);
        $em->persist($notification);
        $em->flush();

1 个答案:

答案 0 :(得分:0)

你得到的错误就像改变

message:
    targetEntity: Messages
    joinColumn:
        name: id_event
        referencedColumnName: id

message:
    targetEntity: Message
    joinColumn:
        name: id_event
        referencedColumnName: id

请注意,一个学说要求entityName为单数,所以如果它是一个集合,它会自动复数getter和setter,也许这有点混乱