我正在尝试为我的脚本制作一个论坛模块,我快完成了,但是遇到了一个问题。
当前,这是我的结构:
Forum
可以有多个Topics
(oneToMany)Topic
可以有多个Messages
(oneToMany)主题消息也保存为消息实体。
这是我的Topic
实体:
<?php declare(strict_types = 1);
namespace App\Entity\Forum;
use App\Entity\Player\Player;
use App\Helper\SlugifyHelper;
use Carbon\Carbon;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* Class Topic
* @package App\Entity\Forum
*/
class Topic
{
const VISIBILITY_DELETED = -1;
const VISIBILITY_PENDING = 0;
const VISIBILITY_PUBLISHED = 1;
/**
* @var int|null
*/
private $id;
/**
* @var Forum|null
*/
private $forum;
/**
* @var string|null
*/
private $title;
/**
* @var bool
*
* @Groups({"promote"})
*/
private $announce = false;
/**
* @var int|null
*/
private $totalMessages = 0;
/**
* @var Message|null
*/
private $lastMessage = null;
/**
* @var int|null
*/
private $visibility = self::VISIBILITY_PUBLISHED;
/**
* @var bool
*
* @Groups({"pin"})
*/
private $pinned = false;
/**
* @var bool
*
* @Groups({"status"})
*/
private $closed = false;
/**
* @var \DateTime|null
*/
private $publishedAt;
/**
* @var Message[]|ArrayCollection
*/
private $messages;
/**
* Topic constructor.
*/
public function __construct()
{
$this->messages = new ArrayCollection();
$this->publishedAt = Carbon::now();
}
/**
* @return int|null
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @param int|null $id
*
* @return Topic
*/
public function setId(?int $id): Topic
{
$this->id = $id;
return $this;
}
/**
* @return Forum|null
*/
public function getForum(): ?Forum
{
return $this->forum;
}
/**
* @param Forum|null $forum
*
* @return Topic
*/
public function setForum(?Forum $forum): Topic
{
$this->forum = $forum;
return $this;
}
/**
* @return string|null
*/
public function getTitle(): ?string
{
return $this->title;
}
/**
* @param string|null $title
*
* @return Topic
*/
public function setTitle(?string $title): Topic
{
$this->title = $title;
return $this;
}
/**
* @return bool
*/
public function isAnnounce(): bool
{
return $this->announce;
}
/**
* @param bool $announce
*
* @return Topic
*/
public function setAnnounce(bool $announce): Topic
{
$this->announce = $announce;
return $this;
}
/**
* @return int|null
*/
public function getTotalMessages(): ?int
{
return $this->totalMessages;
}
/**
* @param int|null $totalMessages
*
* @return Topic
*/
public function setTotalMessages(?int $totalMessages): Topic
{
$this->totalMessages = $totalMessages;
return $this;
}
/**
* @return Message|null
*/
public function getLastMessage(): ?Message
{
return $this->lastMessage;
}
/**
* @param Message|null $lastMessage
*
* @return Topic
*/
public function setLastMessage(?Message $lastMessage): Topic
{
$this->lastMessage = $lastMessage;
return $this;
}
/**
* @return int|null
*/
public function getVisibility(): ?int
{
return $this->visibility;
}
/**
* @param int|null $visibility
*
* @return Topic
*/
public function setVisibility(?int $visibility): Topic
{
$this->visibility = $visibility;
return $this;
}
/**
* @return bool
*/
public function isPinned(): bool
{
return $this->pinned;
}
/**
* @param bool $pinned
*
* @return Topic
*/
public function setPinned(bool $pinned): Topic
{
$this->pinned = $pinned;
return $this;
}
/**
* @return bool
*/
public function isClosed(): bool
{
return $this->closed;
}
/**
* @param bool $closed
*
* @return Topic
*/
public function setClosed(bool $closed): Topic
{
$this->closed = $closed;
return $this;
}
/**
* @return \DateTime|null
*/
public function getPublishedAt(): ?\DateTime
{
return $this->publishedAt;
}
/**
* @param \DateTime|null $publishedAt
*
* @return Topic
*/
public function setPublishedAt(?\DateTime $publishedAt): Topic
{
$this->publishedAt = $publishedAt;
return $this;
}
/**
* @return Message[]|ArrayCollection
*/
public function getMessages()
{
return $this->messages;
}
/**
* @param Message[]|ArrayCollection $messages
*
* @return Topic
*/
public function setMessages($messages)
{
$this->messages = $messages;
return $this;
}
/**
* @param Message $message
*
* @return Topic
*/
public function addMessage(Message $message): Topic
{
if (!$this->messages->contains($message)) {
$this->messages->add($message);
}
return $this;
}
/**
* @param Message $message
*
* @return Topic
*/
public function removeMessage(Message $message): Topic
{
if ($this->messages->contains($message)) {
$this->messages->removeElement($message);
}
return $this;
}
/**
* @return bool
*/
public function isDeleted(): bool
{
return self::VISIBILITY_DELETED === $this->visibility;
}
/**
* @return array
*/
public function getUrlParameters(): array
{
return [
'id' => $this->id,
'topicSlug' => SlugifyHelper::slugify($this->title)
];
}
/**
* @return Player|null
*/
public function getAuthor(): ?Player
{
if ($this->messages->count() > 0) {
/**
* @var $message Message
*/
$message = $this->messages->first();
return $message->getPlayer();
}
return null;
}
/**
* @return Message|null
*/
public function getMessage(): ?Message
{
if ($this->messages->count() > 0) {
return $this->messages->first();
}
return null;
}
/**
* @return Topic
*/
public function increaseTotalMessages(): Topic
{
$this->totalMessages++;
return $this;
}
/**
* @return Topic
*/
public function decreaseTotalMessages(): Topic
{
$this->totalMessages--;
return $this;
}
/**
* @return array
*/
public static function getVisibilityStatuses(): array
{
return [
self::VISIBILITY_DELETED => 'Deleted',
self::VISIBILITY_PENDING => 'Pending',
self::VISIBILITY_PUBLISHED => 'Published'
];
}
/**
* {@inheritdoc}
*/
public function __toString()
{
return $this->title ?? '-';
}
}
这是我的Message
实体:
<?php declare(strict_types = 1);
namespace App\Entity\Forum;
use App\Entity\Player\Player;
use App\Helper\SlugifyHelper;
use Carbon\Carbon;
/**
* Class Message
* @package App\Entity\Forum
*/
class Message
{
/**
* @var int|null
*/
private $id;
/**
* @var Topic|null
*/
private $topic;
/**
* @var Forum|null
*/
private $forum;
/**
* @var Player|null
*/
private $player;
/**
* @var string|null
*/
private $message;
/**
* @var \DateTime|null
*/
private $publishedAt;
/**
* Message constructor.
*/
public function __construct()
{
$this->publishedAt = Carbon::now();
}
/**
* @return int|null
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @param int|null $id
*
* @return Message
*/
public function setId(?int $id): Message
{
$this->id = $id;
return $this;
}
/**
* @return Topic|null
*/
public function getTopic(): ?Topic
{
return $this->topic;
}
/**
* @param Topic|null $topic
*
* @return Message
*/
public function setTopic(?Topic $topic): Message
{
$this->topic = $topic;
return $this;
}
/**
* @return Forum|null
*/
public function getForum(): ?Forum
{
return $this->forum;
}
/**
* @param Forum|null $forum
*
* @return Message
*/
public function setForum(?Forum $forum): Message
{
$this->forum = $forum;
return $this;
}
/**
* @return Player|null
*/
public function getPlayer(): ?Player
{
return $this->player;
}
/**
* @param Player|null $player
*
* @return Message
*/
public function setPlayer(?Player $player): Message
{
$this->player = $player;
return $this;
}
/**
* @return string|null
*/
public function getMessage(): ?string
{
return $this->message;
}
/**
* @param string|null $message
*
* @return Message
*/
public function setMessage(?string $message): Message
{
$this->message = $message;
return $this;
}
/**
* @return \DateTime|null
*/
public function getPublishedAt(): ?\DateTime
{
return $this->publishedAt;
}
/**
* @param \DateTime|null $publishedAt
*
* @return Message
*/
public function setPublishedAt(?\DateTime $publishedAt): Message
{
$this->publishedAt = $publishedAt;
return $this;
}
public function getShortMessage(): ?string
{
return SlugifyHelper::shortText($this->message, 100);
}
/**
* {@inheritdoc}
*/
public function __toString()
{
return $this->message ?? '-';
}
}
这是管理员代码(我正在使用最新版本的Sonata Admin)。
protected function configureFormFields(FormMapper $form)
{
$form
->with('General', [
'box_class' => 'box box-custom',
'class' => 'col-md-6 col-sm-12 col-xs-12'
])
->add('forum', ModelAutocompleteType::class, [
'property' => 'title',
'attr' => [
'placeholder' => 'Search forum'
]
])
->add('title', TextType::class, [
'attr' => [
'placeholder' => 'Title'
]
])
->add('message.player', ModelAutocompleteType::class, [
'label' => 'Author',
'property' => 'username',
'attr' => [
'placeholder' => 'Search player'
],
], [
'admin_code' => PlayerAdmin::class,
])
->add('message.message', TextareaType::class, [
'label' => 'Message',
'attr' => [
'placeholder' => 'Message',
'rows' => 6,
'class' => 'js-summernote'
]
])
->end()
->with('Configuration', [
'box_class' => 'box box-custom',
'class' => 'col-md-6 col-sm-12 col-xs-12'
])
->add('visibility', ChoiceType::class, [
'choices' => array_flip(Topic::getVisibilityStatuses())
])
->add('announce', CheckboxType::class, [
'required' => false,
])
->add('pinned', CheckboxType::class, [
'required' => false,
])
->add('closed', CheckboxType::class, [
'required' => false,
])
->end();
}
让我们假设我仅使用此代码来编辑主题实体(我知道当前代码不适用于创建新主题)。
这是我的错误:
当前字段
message__player
未链接到管理员。请 为目标实体创建一个:
我做错了什么?