我有两个实体:问题和注释,并且一个问题可以有多个注释。我这样定义它们:
class Issue {
// ...
/**
* @ORM\OneToMany(targetEntity="Note", mappedBy="issue")
*/
protected $notes;
public function getNotes() {
return $this->notes->toArray();
}
public function addNote($note) {
if (!$this->notes->contains($note)) $this->notes->add($note);
}
public function removeNote($note) {
if ($this->notes->contains($note)) $this->notes->removeElement($note);
}
}
class Note {
// ...
/**
* @ORM\Column(type="string", nullable=false)
*/
protected $description;
/**
* @ORM\ManyToOne(targetEntity="Issue", inversedBy="notes")
* @ORM\JoinColumn(name="issue", referencedColumnName="id", nullable=false)
*/
protected $issue;
public function getDescription() // ...
public function setDescription() // ...
public function getIssue() // ...
public function setIssue() // ...
}
我定义了一个 IssueType 来创建嵌入 NoteType 表单的表单:
class IssueType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
// ...
->add('notes', 'collection', ['type' => new NoteType()]);
}
}
class NoteType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->add('description', 'textarea');
}
}
当我想创建一个新问题时,这很有效,因为注释数组仅包含由 IssueController 创建的一个(空白)注释,用户可以使用表单填写该注释。但是,创建问题后,我会看到一个显示所有注释的查看问题页面,并且底部有一个用于添加新注释的表单。问题在于收集表单为新的(空白)笔记以及所有以前的笔记创建了一个输入(见下图)。
有什么方法可以让我只使用 Symfony 包含新(空白)注释表单的输入,还是需要使用 JavaScript 删除以前的注释?
编辑:
这是IssueController的代码:
public function newAction(Request $request) {
$em = $this->getDoctrine()->getManager();
$issue = new Issue();
$note = new Note();
$issue->addNote($note);
$note->setIssue($issue);
$form = $this->createForm('issue', $issue);
$form->handleRequest($request);
// ...
if ($form->isValid()) {
$em->persist($issue);
$em->persist($note);
$em->flush();
return $this->redirectToRoute(...);
}
return $this->render('/issue/new.html.twig', [
'issue' => $issue,
'form' => $form->createView()
]);
}
最佳答案
出现注释编辑框是因为您的表单指示为一对多关系创建可编辑集合。将某些内容放入表单类型意味着它通常是可编辑的,或者至少以表单形式呈现。
如果您希望表单只能添加新注释,则必须删除表单的该集合属性。
而不是
->add('notes', 'collection', ['type' => new NoteType()]);
有
->add('newNote', 'textarea', ['label' => 'Add note', 'mapped' => false];
您的 Controller 也需要修改。
public function newAction(Request $request) {
$em = $this->getDoctrine()->getManager();
$issue = new Issue();
$form = $this->createForm('issue', $issue);
$form->handleRequest($request);
if ($form->isValid()) {
if ($newNote = trim($form['newNote']->getData())) {
$note = new Note();
$issue->addNote($note);
$note->setIssue($issue);
$note->setDescription($newNote);
$em->persist($note); // Probably not needed as you're cascading persist
}
$em->persist($issue);
$em->flush();
return $this->redirectToRoute(...);
}
return $this->render('/issue/new.html.twig', [
'issue' => $issue,
'form' => $form->createView()
]);
}
这只会显示新注释的输入。要显示现有笔记,您需要在 View 中执行此操作,例如:
<h2>Notes</h2>
{% for note in issue.notes if note.description %}
<p>{{ loop.index }}: {{ note.description }}</p>
{% else %}
<p>No notes have been added for this issue.</p>
{% endfor %}
关于javascript - 包含最后一个数组元素的表单集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33037877/