我正在尝试创建一个基于symfony的论坛。在索引页上,我需要向用户显示他们可以选择进入的部分。然而,每个部分都有类别,所有这些都需要显示在一个视图中。它应该是这样的。
- Section1
- category1
- category2
- Section2
- category3
等。
我正在遵循symblog.co.uk教程,并尝试根据他们的博客/评论的例子来做,有一个简单的问题,他们在显示动作中定义评论,每个博客都有$comments变量,我需要我的类别可以从$sections变量访问。对于每个Section用户必须能够读取类别和添加新的
我的文件是这样的
index action,基本视图,我写的所有内容都发生在这里
{% 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?
Section &类别。
实际上你有一个section属性在你的Category实体中它被映射为ManyToOne,对吧?
现在,你需要一个category属性在你的Section属性中,它被映射为OneToMany
这样,您就可以从Section中访问Category,这样就可以在不使用任何请求的情况下显示它们。
有一个例子,我有两个实体,Division &老师。一个老师可以教多个部门的学生。这是我在Division实体
中的映射/**
*
* @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->setDivision($division) &$division->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到您的表单。在Category类型中,像这样添加一行=>
->add('section', 'entity', array('class' => 'YourBundle:Section', 'property' => 'name','label' => 'Section'))
它将允许您为这个类别选择您想要的部分。
希望我有帮助,抱歉我的英语。