Symfony2 TWIG显示来自已知ID的用户

时间:2014-07-10 13:35:50

标签: php mysql sql symfony doctrine-orm

我有一个存储在doctrine数据库中的任务列表,我这样返回:

$task = $this->getDoctrine()
    ->getRepository('SeotoolMainBundle:Tasks')
    ->findAll();
return array('form' => $form->createView(), 'message' => '', 'list_tasks' => $task);

在一行中存储第二个表的ID和条目。我不想只输出ID,而是要存储在另一个表中的Name,Description等。正常的PHP& MYSQL这可以通过使用JOIN来完成 - 我如何使用Symfony& TWIG?

Twig输出:

{% for task in list_tasks%}
    {{ task.Id }}
    {{ task.TaskTitle }}
    {{ task.TaskDescription }}
    {{ task.TaskTypes }} /* Here I want not only get the ID but also other fields stored in the database with TaskType ID = task.TaskTypes */
    {{ task.User }}  /* Here I want not only get the ID but also other fields stored in the database with User ID = task.User */
{% endfor %}

2 个答案:

答案 0 :(得分:0)

我假设TaskTypeUser是实体,属于Tasks实体。在这种情况下,请尝试以下操作。请注意,我的示例中只有一个id为1的任务:

$task = $this->getDoctrine()->getRepository('SeotoolMainBundle:Tasks')->find(1);
$taskTypes = $task->getTaskTypes();
$user = $task->getUser();

return array(
    'form' => $form->createView(),
    'message' => '',
    'list_tasks' => $task,
    'task_types' => $taskTypes,
    'user' => $user,
);

在你的树枝上:

{% for task_type in task_types %}
    {{ task_type.Id }} // or whatever fields a task_type has
{% endfor %}

user

也一样

编辑:

由于您希望一次处理所有任务,我想知道以下是否会起作用:

{% for task_list in task_lists %}
    {% for task_type in task_list.taskType %}
        {{ task_type.Id }} // or whatever fields a task_type has
    {% endfor %}
{% endfor %}

答案 1 :(得分:0)

我现在就这样做了,但我不认为,它是100%正确并且符合。

如何做得更好?幸运的是,这实际上对我有用:

$task = $this->getDoctrine()
    ->getRepository('SeotoolMainBundle:Tasks')
    ->findAll();

$taskTypes = $this->getDoctrine()
    ->getRepository('SeotoolMainBundle:TaskTypes')
    ->findAll();

$user = $this->getDoctrine()
    ->getRepository('SeotoolMainBundle:User')
    ->findAll();

return array(
    'form' => $form->createView(),
    'message' => '',
    'list_tasks' => $task,
    'list_task_types' => $taskTypes,
     'list_user' => $user
);

TWIG:

{% for task in list_tasks %}

    Task ID: {{ task.ID }}    <br/>

    {% for type in list_task_types if type.id == task.tasktypes %}
        {{ type.tasktypetitle }} <br/>
    {% endfor %}

    {% for user in list_user if user.id == task.user %}
        {{ user.username }} <br/>
    {% endfor %}

    <hr/>

{% endfor %}
相关问题