我正在尝试创建一个基于symfony的论坛。在索引页面上,我需要向用户显示他们可以选择输入的部分。但是每个部分都有类别,所有这些都需要在一个视图中显示。它看起来应该是这样的。
等
我按照symblog.co.uk教程,并尝试根据他们的blogs/comments example进行操作,有一个简单的问题,他们在show action中定义注释,每个博客都有$ comments变量,我需要从$ sections变量访问我的类别。 对于每个部分,用户必须能够读取类别并添加新的
这里是我的文件的样子。
索引操作,基本视图,我写的所有内容都发生了
{% block body %}
IndexAction of Page Controller
<form action="{{ path("EpiForumBundle_section_create") }}" method="post" {{ form_enctype(form) }} class="section">
{{ form_errors(form) }}
{{ form_row(form.name) }}
{{ form_rest(form) }}
<input type="submit" value="Submit" />
</form>
<table>
<th>date created
<th>name
{% for section in sections %}
<tr>
<td>
<div class="date"><time datetime="{{ section.created|date('c') }}">{{ section.created|date('l, F j, Y') }}</time></div>
</td>
<td>
<p><span class="highlight">{{ section.name }}</span></p>
</td>
</tr>
{% else %}
<p>There are no sections for this forum</p>
{% endfor %}
</table>
{% endblock %}
页面控制器
<?php
namespace Epi\ForumBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Epi\ForumBundle\Entity\Section;
use Epi\ForumBundle\Form\SectionType;
class PageController extends Controller
{
public function indexAction()
{
$section = new Section();
$form = $this->createForm(new SectionType(), $section);
$em = $this->getDoctrine()
->getEntityManager();
$sections = $em->getRepository('EpiForumBundle:Section')
->getLatestSections();
return $this->render('EpiForumBundle:Page:index.html.twig', array('sections' => $sections, 'form' => $form->createView()));
}
}
部门控制器
<?php
namespace Epi\ForumBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Epi\ForumBundle\Entity\Section;
use Epi\ForumBundle\Form\SectionType;
class SectionController extends Controller
{
public function createAction()
{
$section = new Section();
$form = $this->createForm(new SectionType(), $section);
$em = $this->getDoctrine()->getManager();
$request = $this->getRequest();
if($request->getMethod() == 'POST'){
$form->bind($this->getRequest());/*or $form->handleRequest($request); depends on symfony version */
$em->persist($section);
$em->flush();
return $this->redirect("/");
}
return $this->redirect("/");
}
}
部分类型:
<?php
namespace Epi\ForumBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class SectionType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name');
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Epi\ForumBundle\Entity\Section'
));
}
/**
* @return string
*/
public function getName()
{
return 'epi_forumbundle_section';
}
}
那么现在我将这些表格放在哪里?应该如何编写这些表格以了解section_id?
答案 0 :(得分:0)
你需要在Section和amp;之间建立双向关系。分类
Actualy你的Category实体中有一个section属性,它被映射到ManyToOne吗?
现在,您需要在Section属性中使用Categorys属性,该属性映射到OneToMany。
有了它,你可以从Section访问Category,所以使用任何请求显示whitout。
有一个例子,我有两个实体,分部&amp;老师。一位老师可以教授多个部门。 继承我在分部实体中的映射
/**
*
* @ORM\ManyToOne(targetEntity="Acme\DemoBundle\Entity\Teacher", inversedBy="divisions")
* @ORM\JoinColumn(nullable=false)
*/
private $teacher;
在教师实体中
/**
* @ORM\OneToMany(targetEntity="Acme\DemoBundle\Entity\Division", mappedBy="teacher", cascade={"remove"})
*/
private $divisions;
分部中有许多部门,它是一个集合。我们需要一个ArrayCollection,如下所示:
public function __construct()
{
parent::__construct(); (optional)
$this->divisions = new \Doctrine\Common\Collections\ArrayCollection();
}
这是双向的,所以你必须扭曲$ teacher-&gt; setDivision($ division)&amp; $ division-&gt; setTeacher($ teacher),一种最简单的方法就是将这些函数调用到另一个函数。像这样:
/**
* Set teacher
*
* @param Acme\DemoBundle\Entity\Teacher $teacher
*/
public function setTeacher(\Acme\DemoBundle\Entity\Teacher $teacher)
{
$this->teacher = $teacher;
$teacher->setDivision($this);
}
关于第二个问题,关于section_id到你的表单。在您的类别类型中,添加一行,例如this =&gt;
->add('section', 'entity', array('class' => 'YourBundle:Section', 'property' => 'name','label' => 'Section'))
它允许您选择所需的此类别部分。
希望我帮助了,抱歉我的英语。