2017-01-06 7 views
-1

私は関係ManyToOneに問題があります。 は、私は2つの実体を持っている:@ORM \ JoinColum名戻りで呼ばsymfonyのDoctrineエンティティリレーションは保存時にnullデータを返します

 $supportMessageThread = new SupportMessagesThreads(); 
     $supportMessageThread 
       ->setUserId($this->getUser()->getId()) 
       ->setStatus(0) 
       ->setTitle($formData->getTitle()) 
       ->setRecipient($formData->getRecipient()) 
       ->setCreated(new \DateTime()); 

     $supportMessage = new SupportMessages($formData); 
     $supportMessage 
       ->setThreadId($supportMessageThread) 
       ->setCreated(new \DateTime()) 
       ->setIsReadSender(1) 
       ->setIsReadRecipient(0) 
       ->setSender($this->getUser()->getId()) 
       ->setContent($formData->message); 

     $em = $this->getDoctrine()->getManager(); 
     $em->persist($supportMessageThread); 
     $em->persist($supportMessage); 
     $em->flush(); 

私のフィールドは、すべての時間をゼロ:私のコントローラで

namespace MyApp\PanelBundle\Entity; 
use MyApp\PanelBundle\Entity\SupportMessagesThreads; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* SupportMessages 
* 
* @ORM\Table(name="support_messages") 
* @ORM\Entity(repositoryClass="MyApp\PanelBundle\Repository\SupportMessagesRepository") 
*/ 
class SupportMessages 
{ 
    /** 
    * @var int 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @var int 
    * 
    * @ORM\Column(name="thread_id", type="integer") 
    */ 
    private $thread_id; 

    /** 
    * @var int 
    * 
    * @ORM\Column(name="sender", type="integer") 
    */ 
    private $sender; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="content", type="text") 
    */ 
    private $content; 

    /** 
    * @var bool 
    * 
    * @ORM\Column(name="is_read_sender", type="boolean") 
    */ 
    private $is_read_sender; 

    /** 
    * @var bool 
    * 
    * @ORM\Column(name="is_read_recipient", type="boolean") 
    */ 
    private $is_read_recipient; 

    /** 
    * @var \DateTime 
    * 
    * @ORM\Column(name="created", type="datetime") 
    */ 
    private $created; 

    /** 
     * @ORM\ManyToOne(targetEntity="SupportMessagesThreads", inversedBy="messages") 
     * @ORM\JoinColumn(name="thread_id", referencedColumnName="id") 
     */ 
    private $thread; 

    /** 
    * Get id 
    * 
    * @return int 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 


    /** 
    * Set threadId 
    * 
    * @param integer $threadId 
    * 
    * @return SupportMessages 
    */ 
    public function setThreadId($thread_id) 
    { 
     $this->thread_id = $thread_id; 

     return $this; 
    } 


    /** 
    * Get threadId 
    * 
    * @return int 
    */ 
    public function getThreadId() 
    { 
     return $this->thread_id; 
    } 

    /** 
    * Set sender 
    * 
    * @param integer $sender 
    * 
    * @return SupportMessages 
    */ 
    public function setSender($sender) 
    { 
     $this->sender = $sender; 

     return $this; 
    } 

    /** 
    * Get sender 
    * 
    * @return int 
    */ 
    public function getSender() 
    { 
     return $this->sender; 
    } 

    /** 
    * Set content 
    * 
    * @param string $content 
    * 
    * @return SupportMessages 
    */ 
    public function setContent($content) 
    { 
     $this->content = $content; 

     return $this; 
    } 

    /** 
    * Get content 
    * 
    * @return string 
    */ 
    public function getContent() 
    { 
     return $this->content; 
    } 

    /** 
    * Set isReadSender 
    * 
    * @param boolean $isReadSender 
    * 
    * @return SupportMessages 
    */ 
    public function setIsReadSender($isReadSender) 
    { 
     $this->is_read_sender = $isReadSender; 

     return $this; 
    } 

    /** 
    * Get isReadSender 
    * 
    * @return bool 
    */ 
    public function getIsReadSender() 
    { 
     return $this->is_read_sender; 
    } 

    /** 
    * Set isReadRecipient 
    * 
    * @param boolean $isReadRecipient 
    * 
    * @return SupportMessages 
    */ 
    public function setIsReadRecipient($isReadRecipient) 
    { 
     $this->is_read_recipient = $isReadRecipient; 

     return $this; 
    } 

    /** 
    * Get isReadRecipient 
    * 
    * @return bool 
    */ 
    public function getIsReadRecipient() 
    { 
     return $this->is_read_recipient; 
    } 

    /** 
    * Set created 
    * 
    * @param \DateTime $created 
    * 
    * @return SupportMessages 
    */ 
    public function setCreated($created) 
    { 
     $this->created = $created; 

     return $this; 
    } 

    /** 
    * Get created 
    * 
    * @return \DateTime 
    */ 
    public function getCreated() 
    { 
     return $this->created; 
    } 
    } 

<?php 

namespace MyApp\PanelBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\ArrayCollection; 
use MyApp\PanelBundle\Entity\SupportMessages; 
/** 
* SupportMessagesThreads 
* 
* @ORM\Table(name="support_messages_threads") 
* @ORM\Entity(repositoryClass="MyApp\PanelBundle\Repository\SupportMessagesThreadsRepository") 
*/ 
class SupportMessagesThreads 
{ 
    /** 
    * @var int 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @var int 
    * 
    * @ORM\Column(name="user_id", type="integer") 
    */ 
    private $user_id; 

    /** 
    * @var int 
    * 
    * @ORM\Column(name="recipient", type="integer") 
    */ 
    private $recipient; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="title", type="string") 
    */ 
    private $title; 

    /** 
    * @var int 
    * 
    * @ORM\Column(name="status", type="integer") 
    */ 
    private $status; 

    /** 
    * @var \DateTime 
    * 
    * @ORM\Column(name="created", type="datetime") 
    */ 
    private $created; 

    /** 
     * @ORM\OneToMany(targetEntity="SupportMessages", mappedBy="thread") 
     */ 
    protected $messages; 


    public function __construct() 
    { 
     $this->messages = new ArrayCollection(); 
    } 
    /** 
    * Get id 
    * 
    * @return int 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    function getMessages() { 
     return $this->messages; 
    } 

    function setMessages($messages) { 
     $this->messages = $messages; 
    } 

    /** 
    * Set userId 
    * 
    * @param integer $userId 
    * 
    * @return SupportMessagesThreads 
    */ 
    public function setUserId($userId) 
    { 
     $this->user_id = $userId; 

     return $this; 
    } 

    /** 
    * Get userId 
    * 
    * @return int 
    */ 
    public function getUserId() 
    { 
     return $this->user_id; 
    } 

    /** 
    * Set recipient 
    * 
    * @param integer $recipient 
    * 
    * @return SupportMessagesThreads 
    */ 
    public function setRecipient($recipient) 
    { 
     $this->recipient = $recipient; 

     return $this; 
    } 

    /** 
    * Get recipient 
    * 
    * @return int 
    */ 
    public function getRecipient() 
    { 
     return $this->recipient; 
    } 

    /** 
    * Set title 
    * 
    * @param string $title 
    * 
    * @return SupportMessagesThreads 
    */ 
    public function setTitle($title) 
    { 
     $this->title = $title; 

     return $this; 
    } 

    /** 
    * Get title 
    * 
    * @return string 
    */ 
    public function getTitle() 
    { 
     return $this->title; 
    } 

    /** 
    * Set status 
    * 
    * @param integer $status 
    * 
    * @return SupportMessagesThreads 
    */ 
    public function setStatus($status) 
    { 
     $this->status = $status; 

     return $this; 
    } 

    /** 
    * Get status 
    * 
    * @return int 
    */ 
    public function getStatus() 
    { 
     return $this->status; 
    } 

    /** 
    * Set created 
    * 
    * @param \DateTime $created 
    * 
    * @return SupportMessagesThreads 
    */ 
    public function setCreated($created) 
    { 
     $this->created = $created; 

     return $this; 
    } 

    /** 
    * Get created 
    * 
    * @return \DateTime 
    */ 
    public function getCreated() 
    { 
     return $this->created; 
    } 
    } 

は、私はこのコードを持っています。 "thread_id"を他のフィールドに変更すると"sender"フィールドはnullです。 SupportMessageThreadエンティティのIDをthread_idに設定するために何ができるのですか?

印刷データは正常に動作します。私がテスト記録を手動で置くと、次のイムはdoctrineで取得します。すべてがOKです。この問題は、保存時にのみ発生します。

私を助けてください:((

答えて

0

は、おそらくあなたがCascade operations

を使用すると、あなたのマッピングは少し奇妙に見えるあなたは整数

/** 
* Set threadId 
* 
* @param integer $threadId 
* 
* @return SupportMessages 
*/ 
public function setThreadId($thread_id) 
{ 
    $this->thread_id = $thread_id; 

    return $this; 
} 

しかしSupportMessagesThreadsされる引数を宣言することに注意してください。合格:

->setThreadId($supportMessageThread) 

オブジェクト代わりに

/** 
* Set thread 
* 
* @param SupportMessagesThreads $thread 
* 
* @return SupportMessages 
*/ 
public function setThread(SupportMessagesThreads $thread) 
{ 
    $this->thread = $thread; 

    return $this; 
} 

のようなスカラーSとSupportMessagesThreads

から$ thread_idはフィールドを削除
関連する問題