我正在尝试实现FOSMessageBundle,但每当我尝试访问任何消息或非空消息列表(发送/接收/删除)时,我都会收到此错误:
Fatal error: Call to a member function getId() on null
堆栈显示为生成的
in vendor/friendsofsymfony/message-bundle/FOS/MessageBundle/Model/Message.php at line 160
public function getMetadataForParticipant(ParticipantInterface $participant)
{
foreach ($this->metadata as $meta) {
if ($meta->getParticipant()->getId() == $participant->getId()) {
return $meta;
}
}
我不知道为什么它无法获取参与者信息。数据已正确保存在数据库中,我已根据文档按照安装说明进行操作。
这是我的用户实体:
namespace AppBundle\Entity\Security;
use Doctrine\ORM\Mapping\UniqueConstraint;
use FOS\MessageBundle\Model\ParticipantInterface;
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* User
*
* @ORM\Table("fos_user", uniqueConstraints={@UniqueConstraint(name="UIDX_email_isArchived", columns={"is_archived", "email"})}))
* @ORM\Entity
* @ORM\Entity(repositoryClass="AppBundle\Repository\Security\UserRepository")
*/
class User extends BaseUser implements ParticipantInterface
{
//region Properties
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var GroupMember
*
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Security\GroupMember", mappedBy="member", cascade={"persist", "remove"}, orphanRemoval=TRUE)
*/
protected $groupMembers;
/**
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Security\Group")
* @ORM\JoinTable(name="fos_user_user_group",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
* )
*/
protected $groups;
/**
* @var string
*
* @ORM\Column(name="displayName", type="string", length=512, nullable=true)
*/
private $displayName;
/**
* @var bool
*
* @ORM\Column(name="is_archived", type="boolean", options={"default" = "0"})
*/
private $isArchived;
//endregion
//region GettersAndSetters
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @return GroupMember
*/
public function getGroupMembers()
{
return $this->groupMembers->toArray();
}
/**
* @param GroupMember $groupMembers
*/
public function setGroupMembers($groupMembers)
{
$this->groupMembers = $groupMembers;
}
/**
* @return string
*/
public function getDisplayName()
{
return $this->displayName;
}
/**
* @param string $displayName
*/
public function setDisplayName($displayName)
{
$this->displayName = $displayName;
}
/**
* @return boolean
*/
public function isIsArchived()
{
return $this->isArchived;
}
/**
* @param boolean $isArchived
*/
public function setIsArchived($isArchived)
{
$this->isArchived = $isArchived;
}
//endregion
//region Methods
public function __construct()
{
parent::__construct();
$this->groupMembers = new ArrayCollection();
$this->departments = new ArrayCollection();
$this->pools = new ArrayCollection();
$this->setIsArchived(false);
}
/* Override FOSUB */
public function setEmail($email)
{
parent::setEmail($email);
$this->username = $email;
return $this;
}
public function setEmailCanonical($emailCanonical)
{
parent::setEmailCanonical($emailCanonical);
$this->usernameCanonical = $emailCanonical;
return $this;
}
public function serialize()
{
//return parent::serialize(); // TODO: Change the autogenerated stub
return serialize(array(
$this->id,
$this->username,
$this->email,
));
}
public function unserialize($serialized)
{
//parent::unserialize($serialized); // TODO: Change the autogenerated stub
list (
$this->id,
$this->username,
$this->email,
) = unserialize($serialized);
}
//endregion
}
我的config.yml:
fos_message:
db_driver: orm
thread_class: AppBundle\Entity\Thread
message_class: AppBundle\Entity\Message
消息类:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use FOS\MessageBundle\Entity\Message as BaseMessage;
/**
* @ORM\Entity
*/
class Message extends BaseMessage
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(
* targetEntity="AppBundle\Entity\Thread",
* inversedBy="messages"
* )
* @var \FOS\MessageBundle\Model\ThreadInterface
*/
protected $thread;
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Security\User")
* @var \FOS\MessageBundle\Model\ParticipantInterface
*/
protected $sender;
/**
* @ORM\OneToMany(
* targetEntity="AppBundle\Entity\MessageMetadata",
* mappedBy="message",
* cascade={"all"}
* )
* @var MessageMetadata[]|\Doctrine\Common\Collections\Collection
*/
protected $metadata;
}
线程类:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use FOS\MessageBundle\Entity\Thread as BaseThread;
/**
* @ORM\Entity
*/
class Thread extends BaseThread
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Security\User")
* @var \FOS\MessageBundle\Model\ParticipantInterface
*/
protected $createdBy;
/**
* @ORM\OneToMany(
* targetEntity="AppBundle\Entity\Message",
* mappedBy="thread"
* )
* @var Message[]|\Doctrine\Common\Collections\Collection
*/
protected $messages;
/**
* @ORM\OneToMany(
* targetEntity="AppBundle\Entity\ThreadMetadata",
* mappedBy="thread",
* cascade={"all"}
* )
* @var ThreadMetadata[]|\Doctrine\Common\Collections\Collection
*/
protected $metadata;
}