我正在尝试在邮件中插入类别类型。问题在于它不会添加到邮件中。
这是我在MessageController中的帖子:
public function postMessage(Request $request)
{
$message = new Message();
$form = $this->createForm(MessageType::class, $message);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$message->setDate(new \DateTime());
$message->setDownVotes(0);
$message->setUpVotes(0);
if($message->getUser() != null) {
$message->setUser($this->getDoctrine()->getManager()->getRepository(User::class)->find($message->getUser()->getId()));
} else {
return $this->redirectToRoute('loginroute');
}
$entityManager->persist($message);
$entityManager->flush();
return $this->redirectToRoute('getAllMessages');
}
}
在我看来,我会像这样创建一个选择字段:
{{ form_widget(categoryForm.name, { 'attr': {'class': 'mdl-textfield__input'} }) }}
<label for="message_content" class="mdl-textfield__label">Voeg bericht toe</label>
其中哪个是使用CategoryType通过createForm创建的:
class CategoryType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', EntityType::class, array(
'class' => Category::class,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('m')->orderBy('m.name', "ASC");
},
'choice_label' => 'name','label' => false));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Category::class,
]);
}
}
我应该如何在数据库中插入多对多关系类别?
编辑消息实体:
/**
* @ORM\Entity(repositoryClass="App\Repository\MessageRepository")
*/
class Message
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="text")
*/
private $content;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Category", cascade={"persist"}, inversedBy="messages")
*/
private $categories;
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="text")
*/
private $content;
----
public function __construct()
{
$this->comments = new ArrayCollection();
$this->categories = new ArrayCollection();
}
/**
* @return Collection|Category[]
*/
public function getCategories(): Collection
{
return $this->categories;
}
public function addCategory(Category $category)
{
$category->addMessage($this);
/*if (!$this->categories->contains($category)) {
$this->categories[] = $category;
}
return $this;*/
}
public function removeCategory(Category $category): self
{
if ($this->categories->contains($category)) {
$this->categories->removeElement($category);
}
return $this;
}
public function __toString()
{
return "Entity Message, id= " . $this->getId();
}
类别实体:
/**
* @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
*/
class Category
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Message", mappedBy="categories")
*/
private $messages;
public function __construct()
{
$this->messages = new ArrayCollection();
}
public function create(int $id, string $name)
{
$this->setName($name);
$this->$id=$id;
$this->messages = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
/**
* @return Collection|Message[]
*/
public function getMessages(): Collection
{
return $this->messages;
}
public function addMessage(Message $message)
{
if (!$this->messages->contains($message)) {
$this->messages->add($message);
}
/* if (!$this->messages->contains($message)) {
$this->messages[] = $message;
$message->addCategory($this);
}
return $this;*/
}
public function removeMessage(Message $message): self
{
if ($this->messages->contains($message)) {
$this->messages->removeElement($message);
$message->removeCategory($this);
}
return $this;
}
public function __toString()
{
return "Entity Category, name= " . $this->getName();
}
}
编辑2:
class MessageType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('id', HiddenType::class, array('label' => false))
->add('content', TextType::class, array('required' => false, 'label' => false))
->add('date', HiddenType::class, array('required' => false, 'label' => false))
->add('upVotes', HiddenType::class, array('required' => false, 'label' => false))
->add('downVotes', HiddenType::class, array('required' => false, 'label' => false))
->add('categories', CollectionType::class, array(
'entry_type' => CategoryType::class,
'entry_options' => array('label' => false),
'allow_add' => true,
'by_reference' => false
))
->add('user', CommentUserType::class, array('required' => false, 'label' => false));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Message::class,
]);
}
}