简体   繁体   中英

Symfony2 TWIG show user from know ID

I have a list of tasks stored in the doctrine database which I return like this:

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

In one row the ID of and entry from a second table is stored. I don't want only output the ID but also the Name, Description and so on stored in the other table. With normal PHP & MYSQL this would be done by using JOIN - how can I do it with Symfony & TWIG ?

Twig Output:

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

I am assuming that TaskType and User are entities, which belong within the Tasks entity. In which case, try the following. Note that I am getting just one task with id of 1 in my example:

$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,
);

And in your Twig:

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

And the same for the user

Edit:

As you are wanting to have ALL tasks processed at once, I wonder if simply the following willl work:

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

I did it now on this way, but I dont think, that it is 100% correct and conform.

How to do better? Luckily this works for me actually:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM