TWIG 中的 Symfony2 数据库结果 [英] Symfony2 Database results in TWIG

查看:26
本文介绍了TWIG 中的 Symfony2 数据库结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中有 3 个表:

I have 3 tables in DB:

task_estimation_fields:

CREATE TABLE `task_estimation_fields` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(45) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name_UNIQUE` (`name`)
) ENGINE=InnoDB;

task_estimations:

CREATE TABLE `task_estimations` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `task_id` int(11) NOT NULL,
  `task_estimation_field_id` int(11) NOT NULL,
  `description` blob,
  `summary` blob,
  `effort` int(11) NOT NULL,
  `created_at` datetime NOT NULL,
  `created_by` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `g1` (`task_id`),
  KEY `g2` (`created_by`),
  KEY `g3` (`task_estimation_field_id`),
  CONSTRAINT `g1` FOREIGN KEY (`task_id`) REFERENCES `tasks` (`id`) ON DELETE NO ACTION         ON UPDATE NO ACTION,
  CONSTRAINT `g2` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `g3` FOREIGN KEY (`task_estimation_field_id`) REFERENCES     `task_estimation_fields` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB;

任务:

CREATE TABLE `tasks` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `assignee_id` int(11) NOT NULL,
  `status_id` int(11) NOT NULL,
  `prioryty_id` int(11) NOT NULL,
  `title` varchar(45) NOT NULL,
  `description` longblob,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;

使用以下命令从现有数据库生成实体文件:

Entity files generated from existing database using following commands:

$ php app/console doctrine:mapping:import --force AcmeBlogBundle xml
$ php app/console doctrine:mapping:convert annotation ./src
$ php app/console doctrine:generate:entities AcmeBlogBundle

在控制器中,我以这种方式从数据库中获取结果:

In the controler I am getting resulte from database this way:

public function indexAction($id) {
        $estimations = $this->getDoctrine()
                ->getManager()
                ->createQueryBuilder()
                ->select('tef, te')
                ->from('SynapthsisSpecBundle:TaskEstimationFields', 'tef')
                ->leftJoin('SynapthsisSpecBundle:TaskEstimations', 'te', 'WITH', 'te.taskEstimationField = tef.id AND te.task = :id')
                ->setParameter('id', $id)
                ->getQuery()
                ->getResult();

        if (!$this->container->get('request')->isXmlHttpRequest()) {
            return $this->render('SynapthsisSpecBundle:TaskEstimation:index.html.twig', array('id' => $id, 'estimations' => $estimations));
        } else {
            return $this->render('SynapthsisSpecBundle:TaskEstimation:index_ajax.html.twig', array('id' => $id, 'estimations' => $estimations));
        }
    }

Twig 代码在这里:

The Twig code is here:

{% extends 'SynapthsisSpecBundle::layout.html.twig' %}

{% block page_contener %}
    task estimation index {{ id }}
    <hr />
    {% for es in estimations %}
        {{ es.description }}</br>
    {% endfor %}
{% endblock %}

问题是我得到:

Method "description" for object "Synapthsis\SpecBundle\Entity\TaskEstimationFields" does not exist in SynapthsisSpecBundle:TaskEstimation:index.html.twig at line 7

所以我认为我得到了 TaskEstimations 所以我想用这样的代码显示名称"字段:

So I thought that I am getting TaskEstimations so i wanted to show "name" field with code like this:

{% extends 'SynapthsisSpecBundle::layout.html.twig' %}

{% block page_contener %}
    task estimation index {{ id }}
    <hr />
    {% for es in estimations %}
        {% if null != es %}
            {{ es.name }}</br>
        {% else %}
            aaa </br>
        {% endif %}
    {% endfor %}
{% endblock %}

我得到:

Method "effort" for object "Synapthsis\SpecBundle\Entity\TaskEstimationFields" does not exist in SynapthsisSpecBundle:TaskEstimation:index.html.twig at line 7

如何在 TWIG 模板中打印上述查询的结果?

How can I print results of the query above in the TWIG template?

推荐答案

$estimations 变量是一个对象数组 TaskEstimationFields 所以你不能得到 说明.试试这个:

The $estimations variable is an array of Objects TaskEstimationFields so you can't get description. Try this:

将您的查询表单 ->getResult(); 更改为 ->getScalarResult();

Change your query form ->getResult(); to ->getScalarResult();

然后像这样打印工作

{% for es in estimations %}
    {{ es.te_effort }}</br>
{% endfor %}

那么 es 应该是这样的:

Then es should be something like this:

array (size=8)
  'tef_name' => string 'somename' (length=8)
  'tef_id' => int 1
  'te_description' => resource(52, stream)
  'te_summary' => resource(54, stream)
  'te_effort' => int 123
  'te_createdAt' => 
    object(DateTime)[307]
      public 'date' => string '2014-04-30 11:19:20' (length=19)
      public 'timezone_type' => int 3
      public 'timezone' => string 'Europe/Warsaw' (length=13)
  'te_createdBy' => int 1
  'te_id' => int 3

你可以这样打印:

{% for es in estimations %}
    {{ es.tef_name }}</br>
    {{ es.te_summary }}</br>
    (...)
    {{ es.te_createdAt|date('Y-m-d H:i:s') }}
    (...)
{% endfor %}

这篇关于TWIG 中的 Symfony2 数据库结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆