我遇到一种情况,当一个实体与其他实体相关两次时,实体生成器任务没有为相关实体正确创建“添加”功能。我想知道这是否可能是由于我没有正确定义关系或这是一个错误?如果它只是一个发电机问题,那么这不是什么大问题,但如果这对关系产生意想不到的影响,那么这是一个大问题,我需要你的建议来确定发生了什么。
简而言之,用户与Message有两次与ToMany的关系。消息实体具有发送方和接收方,两者都是相关的用户对象。反过来,用户已发送消息并收到消息。用户端的sentmessages和receivedmessages setters未正确生成。
这就是我的yml看起来很简单:
Acme\DemoBundle\Entity\User:
type: entity
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
name:
type: string
oneToMany:
sentmessages:
targetEntity: Message
mappedBy: user
receivedmessages:
targetEntity: Message
mappedBy: user
Acme\DemoBundle\Entity\Message:
type: entity
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
body:
type: string
created_at:
type: datetime
manyToOne:
sender:
targetEntity: User
inversedBy: sentmessages
joinColumn:
name: sender_id
referencedColumnName: id
receiver:
targetEntity: User
inversedBy: receivedmessages
joinColumn:
name: receiver_id
referencedColumnName: id
正如您所看到的,User与Message有两个关系。当我通过实体生成器任务生成实体时,它只创建了一个向用户添加消息的函数,这是一个不正确的函数:
/**
* Add sentmessages
*
* @param Acme\DemoBundle\Entity\Message $sentmessages
*/
public function addMessage(\Acme\DemoBundle\Entity\Message $sentmessages)
{
$this->sentmessages[] = $sentmessages;
}
相反它应该生成两个添加函数,如下所示:
/**
* Add sentmessages
*
* @param Acme\DemoBundle\Entity\Message $sentmessages
*/
public function addSentmessage(\Acme\DemoBundle\Entity\Message $sentmessages)
{
$this->sentmessages[] = $sentmessages;
}
/**
* Add receivedmessages
*
* @param Acme\DemoBundle\Entity\Message $receivedmessages
*/
public function addReceivedmessage(\Acme\DemoBundle\Entity\Message $receivedmessages)
{
$this->receivedmessages[] = $receivedmessages;
}