Symfony2数据库导致TWIG

时间:2014-04-30 06:57:39

标签: php symfony twig

我在DB中有3个表:

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;

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

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

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

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代码在这里:

{% 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 ,所以我想显示“name”字段,代码如下:

{% 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模板中打印上述查询的结果?

1 个答案:

答案 0 :(得分:0)

$estimations变量是一个对象数组TaskEstimationFields,因此您无法获得description。试试这个:

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

然后像这样打印努力

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

然后es应该是这样的:

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 %}