【问题标题】:How to display many to one relation objects in index action如何在索引操作中显示多对一关系对象
【发布时间】:2014-05-27 19:17:16
【问题描述】:

我正在尝试创建一个基于 symfony 的论坛。在索引页面上,我需要向用户展示他们可以选择进入的部分。但是每个部分都有类别,所有这些都需要显示在一个视图中。它应该看起来像这样。

  • 第 1 节
    • 类别1
    • 类别2
  • 第 2 节
    • 类别3

等等

我正在关注 symblog.co.uk 教程,并尝试根据他们的blogs/comments example 来做,有一个简单的问题,他们在 show action 中定义 cmets,每个博客都有 $cmets 变量,我需要我的类别可以从 $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?

【问题讨论】:

    标签: php symfony


    【解决方案1】:

    您需要在 Section 和 Category 之间建立双向关系。

    实际上,您的 Category 实体中有一个 section 属性被映射为 ManyToOne 对吗?

    所以现在,您需要在 Section 属性中添加一个 Categorys 属性,即映射为 OneToMany。

    这样,您可以从 Section 访问 Category,然后使用任何请求向它们展示。

    有一个例子,我有两个实体,Division 和 Teacher。一位老师可以教多个部门。 这是我在 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 进入你的表单。在您的类别类型中,添加这样的一行 =>

    ->add('section', 'entity', array('class' => 'YourBundle:Section', 'property' => 'name','label' => 'Section'))
    

    它将允许您为此类别选择所需的部分。

    希望对我有所帮助,对不起我的英语。

    【讨论】:

      猜你喜欢
      • 2014-07-01
      • 2020-12-22
      • 1970-01-01
      • 1970-01-01
      • 2020-12-26
      • 1970-01-01
      • 1970-01-01
      • 2021-07-10
      • 1970-01-01
      相关资源
      最近更新 更多