【发布时间】:2018-04-06 07:58:48
【问题描述】:
我需要在 symfony3.4 中为一个应用程序进行聊天,所以我创建了一个频道实体和一个消息实体,我建立了一对多关系并在频道中添加了消息,但实体没有持久性包含在意图中。
在消息实体中
/* $channel.
*
* (Many-To-One, Unidirectional)
* @ORM\ManyToOne(targetEntity="Soraya\ChatBundle\Entity\chanEntity",inversedBy="messages")
* @ORM\JoinColumn(name="channel_id", referencedColumnName="id")
*
*/
protected $channel;
public function setChannel(\Soraya\ChatBundle\Entity\chanEntity $channel ){
$this->channel = $channel;
return $this;
}
在 ChanEntity 实体中
/*
* $messages
* (One-To-Many, Bidirectional)
* @ORM\OneToMany(targetEntity="Soraya\ChatBundle\Messages", mappedBy="channel"),cascade={"persist", "remove"})
*
*
*/
protected $messages;
public function __construct() {
parent::__construct();
$this->messages = new ArrayCollection();
}
public function addmessages(\Soraya\ChatBundle\Entity\Messages $message){
$this->messages[]=$message;
$message->setChannel($this);
return $this;
}
在我的控制器中
`public function addMessageAction(Request $request, $idChan, $utilisateur, $message){
$em = $this->getDoctrine()->getManager();
//message
$advertmess = new Messages();
$advertmess->setMessage($message);
$advertmess->setpseudoUtilisateur($utilisateur);
// chan
$advert2 = $em->getRepository('ChatBundle:chanEntity')->find($idChan);
$advert2->addmessages($advertmess);
// entity
$em->persist($advert2);
$em->persist($advertmess);
$em->flush();
//$em->persist($advert);
//$em->flush();
var_dump($advert2);
//var_dump($advertmess);
return new Response("OK");
}`
When i saw the var_dump in the request i can see the entity inside the entity but when i try to access to it after there nothing :
我在 chan 的 var_dump 中可以看到的示例
object(Soraya\ChatBundle\Entity\chanEntity)[790]
protected 'messages' => null
protected 'typesChan' => string 'contact' (length=7)
.......
请求中的示例
object(Soraya\ChatBundle\Entity\chanEntity)[849]
protected 'messages' =>
array (size=1)
0 =>
object(Soraya\ChatBundle\Entity\Messages)[838]
protected 'pseudoUtilisateur' => string 'user' (length=16)
protected 'message' => string 'testword' (length=5)
protected 'channel' =>
&object(Soraya\ChatBundle\Entity\chanEntity)[849]
protected 'id' => int 135
protected 'sync_uuid' => string '64373958-f69e-4b84-8543-aa3ed6b3be19' (length=36)
protected 'isDeleted' => boolean false
protected 'cree_le' =
......
我的实体中的实体不持久我可以访问在 mydatabase 中创建的消息,但不能访问 chan 和消息之间的连接。有人知道可能出了什么问题吗?谢谢。
【问题讨论】:
标签: symfony-3.4