Php Symfony2细枝显示来自已知ID的用户
我有一个任务列表存储在条令数据库中,我返回如下:Php Symfony2细枝显示来自已知ID的用户,php,mysql,sql,symfony,doctrine-orm,Php,Mysql,Sql,Symfony,Doctrine Orm,我有一个任务列表存储在条令数据库中,我返回如下: $task = $this->getDoctrine() ->getRepository('SeotoolMainBundle:Tasks') ->findAll(); return array('form' => $form->createView(), 'message' => '', 'list_tasks' => $task); 在一行中存储第二个表的ID和条目。我不想只输出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 %}
对于用户也是如此
编辑:
由于您希望一次处理所有任务,我想知道以下方法是否可行:
{% 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
);
细枝:
FatalErrorException:错误:调用/var/www/html/seotool/src/seotool/MainBundle/Controller/TasksController.php行75中非对象的成员函数getTaskTypes这意味着$task从未成为对象。这可能是因为您的数据库中没有id为1:->find1的任务,请尝试更改您知道存在的任务的id。但我不希望仅从一个特定id获取,而是从数据库中存储的所有任务获取。使用findAll会引发以下异常:FatalErrorException:Error:调用/var/www/html/seotool/src/seotool/MainBundle/Controller/TasksController.php第75行中非对象上的成员函数getTaskTypes,这是因为在该场景中,$task是一个任务数组。您将希望循环该数组。在我们进一步讨论该解决方案之前,您是否尝试了我答案末尾的快速可能解决方案?是的,我尝试了-不返回任何结果。我真的很困惑…当你没有和Symfony一起工作很长时间的时候,真的很难理解这一切:也许我的整个结构不好?也许我应该使用多个字段来完成所有操作???您的关联设置正确吗,还是只对单独的表进行联接?任务与任务类型之间的关联是什么?我假设是manyToOne,任务与用户之间又是什么?我假设是manyToOne?我没有发现它与manyToOne或manyToOne一起工作。。。如果能用这种方式解决,我会更高兴……但实际上是这样做的:/***@Assert\Typetype=Seotool\MainBundle\Entity\TaskTypes*@ORM\Columntype=string,name=task\u type,nullable=true/protected$TaskTypes;/**@Assert\Typetype=Seotool\MainBundle\Entity\User*@ORM\Columntype=string,nullable=true*/protected$User;实现这一点的最佳方法应该是使用createQuiryBuilder,这样您就可以创建一个连接查询并获取所有必需的数据,然后将它们像普通的MySQL和PHP一样传递给您的小树枝-
{% 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 %}