我在关系方面遇到了问题。我有 2 个实体:
namespace MyAppPanelBundleEntity;
use MyAppPanelBundleEntitySupportMessagesThreads;
use DoctrineORMMapping as ORM;
/**
* SupportMessages
*
* @ORMTable(name="support_messages")
* @ORMEntity(repositoryClass="MyAppPanelBundleRepositorySupportMessagesRepository")
*/
class SupportMessages
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var int
*
* @ORMColumn(name="thread_id", type="integer")
*/
private $thread_id;
/**
* @var int
*
* @ORMColumn(name="sender", type="integer")
*/
private $sender;
/**
* @var string
*
* @ORMColumn(name="content", type="text")
*/
private $content;
/**
* @var bool
*
* @ORMColumn(name="is_read_sender", type="boolean")
*/
private $is_read_sender;
/**
* @var bool
*
* @ORMColumn(name="is_read_recipient", type="boolean")
*/
private $is_read_recipient;
/**
* @var DateTime
*
* @ORMColumn(name="created", type="datetime")
*/
private $created;
/**
* @ORMManyToOne(targetEntity="SupportMessagesThreads", inversedBy="messages")
* @ORMJoinColumn(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 MyAppPanelBundleEntity;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
use MyAppPanelBundleEntitySupportMessages;
/**
* SupportMessagesThreads
*
* @ORMTable(name="support_messages_threads")
* @ORMEntity(repositoryClass="MyAppPanelBundleRepositorySupportMessagesThreadsRepository")
*/
class SupportMessagesThreads
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var int
*
* @ORMColumn(name="user_id", type="integer")
*/
private $user_id;
/**
* @var int
*
* @ORMColumn(name="recipient", type="integer")
*/
private $recipient;
/**
* @var string
*
* @ORMColumn(name="title", type="string")
*/
private $title;
/**
* @var int
*
* @ORMColumn(name="status", type="integer")
*/
private $status;
/**
* @var DateTime
*
* @ORMColumn(name="created", type="datetime")
*/
private $created;
/**
* @ORMOneToMany(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;
}
}
在我的控制器中,我有以下代码:
$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();
我在 @ORM\JoinColum 名称中调用的字段每次都返回 null。当我将"thread_id"更改为其他字段(例如"发件人")时,"发件人"字段为空。我可以做些什么来将支持消息线程实体的 id 设置为 thread_id。
打印数据工作正常。当我手动将测试记录放入基础并且下一个 im 通过原则获得它时 - 一切都很好。仅在保存时出现问题。
请帮帮我:((
可能您想使用级联操作
而且你的映射看起来有点奇怪。请注意,您将参数声明为整数
/**
* 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;
}
并从支持消息线程中删除 $thread_id 字段
就我而言,我对inversed
和mapped by
的定义不正确。在这两种情况下,它都是相同的字段todo
。
在上面的问题中也是如此。
更改后它是
/**
* @ORMOneToMany(targetEntity=MyTodoElement::class, mappedBy="myTodo")
*/
private $myTodoElement;
/**
* @ORMManyToOne(targetEntity=MyTodo::class, inversedBy="myTodoElement")
* @ORMJoinColumn(nullable=false)
*/
private $myTodo;