php - 原则 2 关系错误

标签 php mysql symfony doctrine-orm

我在将记录插入数据库时​​遇到问题。 我有 3 个表,名为 通知: id、id_event、event_id、is_readed 评论: id、评论、接收者、发送者、created_at 消息 id、主题、消息、接收者、发送者、created_at

我想将 notification.id_event 的评论和消息 id 与多对多关系关联起来,如下所示

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

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

当我尝试插入记录时出现以下异常:

Found entity of type Doctrine\Common\Collections\ArrayCollection on association Privatmarket\BusinessBundle\Entity\Notifications#message, but expecting 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();

最佳答案

嗯,你得到的错误读起来就像改变

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

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

请注意,一个学说期望实体名称为单数,因此如果它是一个集合,它会自动将 getter 和 setter 复数化,也许这有点困惑

关于php - 原则 2 关系错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28669266/

相关文章:

Symfony2 DateTime null 接受

php - 在php Mysql中插入urdu数据出错

php - mysql 查询手动工作,但从代码运行时不返回结果

javascript - 如何使用 javascript 和 ajax 检查表单是否已提交以避免多次发送同一表单

mysql - 在 UPDATE 语句子查询中将 IN 更改为 JOIN

带有特殊字符的php mysql搜索

php - 解析 url 并使用最后一个斜杠后面的部分

php - Symfony自定义错误消息

symfony - 包 Doctrine/反射(reflection)被放弃

php - 密码验证不登录只是重新加载登录页面