我在将记录插入数据库时遇到问题。 我有 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/