[英]Symfony2 TWIG show user from know ID
我有一个存储在学说数据库中的任务列表,我将这样返回:
$task = $this->getDoctrine()
->getRepository('SeotoolMainBundle:Tasks')
->findAll();
return array('form' => $form->createView(), 'message' => '', 'list_tasks' => $task);
在第二行的ID和条目中存储一行。 我不仅要输出ID,还希望输出存储在另一个表中的名称,描述等。 对于普通的PHP和MYSQL,这可以通过使用JOIN来完成-如何使用Symfony和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 %}
我假设TaskType
和User
是实体,它们属于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
相同
编辑:
因为您希望一次处理所有任务,所以我想知道以下Willl是否可以正常工作:
{% 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 %}
我现在已经以这种方式做到了,但是我不认为这是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
);
枝条:
{% 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 %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.