Symfony2 / KitpagesDataGridBundle实体操作

时间:2015-07-16 10:48:03

标签: php symfony doctrine-orm query-builder

我正在使用Symfony 2.7和KitpagesDataGridBundle

我正在为一所学校开发一个应用程序,其中我有两个具有1-N关系的实体:Section-Courses。

以下是两个实体:

栏目

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=255, nullable=false)
 */
private $name;

/**
 * @ORM\OneToMany(targetEntity="my\SiteBundle\Entity\Course", mappedBy="section", cascade={"persist"})
 */
private $courses;

课程

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=255)
 */
private $name;

/**
 * @ORM\ManyToOne(targetEntity="my\SiteBundle\Entity\Section", inversedBy="courses")
 * @ORM\JoinColumn(nullable=false)
 */
private $section;

现在,我希望能够从该部分检索链接的“课程”对象,并在Twig模板中迭代它们。目前,由于我无法这样做,我已经创建了一个原型,它将使用查询构建器将课程名称和ID连接到Section的存储库中的字符串,然后将这些连接在Twig模板中解析为这样:

SectionRepository

/**
 * @return \Doctrine\ORM\QueryBuilder
 */
public function getGridQueryBuilder()
{
    $qb = $this->createQueryBuilder('s');
    $qb
        ->select('s, GroupConcat(c.name SEPARATOR \'|\') as courses, GroupConcat(c.id SEPARATOR \'|\') as coursesID')
        ->leftJoin('s.courses', 'c')
        ->groupBy('s.id')
        ->orderBy('s.name');

    return $qb;
}

SectionController

('Dummy [CoursesNames]'字段允许我对课程名称进行搜索)

public function indexAction(Request $request)
{
    $qb = $this->getRepository('mySiteBundle:Section')->getGridQueryBuilder();

    // Grid config
    $gridConfig = new GridConfig();
    $gridConfig
        ->setName('sectionsList')
        ->setQueryBuilder($qb)
        ->setCountFieldName('s.id')
        ->addField(new Field('s.name',['label'=>'Section name', 'filterable'=>true]))
        ->addField(new Field('c.name', ['label'=>'Dummy[CoursesNames]', 'filterable'=>true, 'visible'=>false]));

    // Paginator config
    $this->setGridPaginator($gridConfig, 5);

    return $this->render('mySiteBundle:Section:index.html.twig', ['grid' => $this->getGrid($gridConfig, $request)]);
}

Section Twig模板

我现在有什么:

{% embed kitpages_data_grid.grid.default_twig with {'grid': grid} %}

{% block kit_grid_thead_column %}
    <th>Courses list</th>
{% endblock %}

{% block kit_grid_tbody_column %}
    {% set courses = item['courses']|split('|') %}
    {% set coursesID = item['coursesID']|split('|') %}

    <!-- Courses -->
    <td>
        {% for course in courses %}
            {% if not course is empty %}
                <a href="{{ path ("viewCourse", {"id": coursesID[loop.index0] }) }}" ><span class="label label-primary">{{ course }}</span></a>
            {% else %}
                (No results)
            {% endif %}

        {% endfor %}
    </td>

{% endblock %}

{% endembed %}

我希望能够做到:

{% embed kitpages_data_grid.grid.default_twig with {'grid': grid} %}

{% block kit_grid_thead_column %}
    <th>Courses list</th>
{% endblock %}

{% block kit_grid_tbody_column %}

    <!-- Courses -->
    <td>

        {# here using section object #}

        {% for course in section.courses %}
            <a href="{{ path ("viewCourse", {"id": course.id }) }}" ><span class="label label-primary">{{ course.name }}</span></a>
        {% endfor %}
    </td>

{% endblock %}

{% endembed %}

即使我的原型运行良好,我根本不喜欢解析的东西,并且到目前为止更喜欢使用section对象。

我尝试过像

这样的事情
{{ item['s'].name }}

在模板中,但我收到此错误消息: 密钥“s”用于具有键“s.id,s.name,courses,coursesID”的数组不存在,尽管我在查询构建器中执行了“select('s')”。 / p>

我不知道我想做什么是可能的。否则,我正在考虑放弃KitPages的东西,并在模板中自己构建表格。

谢谢!

1 个答案:

答案 0 :(得分:0)

我发现我想要的东西是不可能的,因为我相信,因为KitPages DataGrid正在使用querybuilder,所以我无法在不必更改捆绑逻辑的情况下检索实体。因此,我回复了发送给Twig的实体,并在表格中迭代了它们的属性。

相关问题