Php 无法访问字符串变量的属性
在我的控制器中,我只是在我的细枝视图上传递一些用户。 以下是我的功能:Php 无法访问字符串变量的属性,php,symfony,twig,Php,Symfony,Twig,在我的控制器中,我只是在我的细枝视图上传递一些用户。 以下是我的功能: public function viewAction($id) { $em = $this->getDoctrine()->getManager(); $planningRepo = $em->getRepository('SimonPediBundle:Planning'); $userRepo = $em->getRepository('Si
public function viewAction($id)
{
$em = $this->getDoctrine()->getManager();
$planningRepo = $em->getRepository('SimonPediBundle:Planning');
$userRepo = $em->getRepository('SimonUserBundle:User');
$user = $this->getUser();
$planningId = $user->getPlanning()->getId();
$planning = $planningRepo->find($planningId);
$users = $userRepo->findByPlanning($planningId);
if (null === $planning) {
throw new NotFoundHttpException("Le planning d'id ".$id. "n'existe pas.");
}
return $this->render('planningAction/planning.html.twig', array('planning' => $planning,
'users' => $users,));
}
这是我的细枝视图,我想在其中显示html表上的一些信息:
<tbody>
<tr>
<th scope="row">Responsable</th>
{% for i in 1..5 %}
{% set assigned_user = ' ' %}
{% for user in users if user.planningday == i%}
{% set assigned_user = user %}
{% endfor %}
<td>{{assigned_user.name}} {{assigned_user.lastname}}</td>
{% endfor %}
</tr>
<tr>
<th scope="row">Description</th>
{% for i in 1..5 %}
{% set assigned_user = ' ' %}
{% for user in users if user.planningday == i%}
{% set assigned_user = user %}
{% endfor %}
<td>{{assigned_user.planningcontent}}</td>
{% endfor %}
</tr>
</tbody>
用这条线
{% set assigned_user = ' ' %}
将“assigned_user”的值设置为“”。
如果您没有符合此条件的用户
{% for user in users if user.planningday == i%}
值将保留为字符串而不是对象。如果使用对象,则无需将默认值设置为字符串。将其设置为
null
并检查是否找到用户
<tbody>
<tr>
<th scope="row">Responsable</th>
{% for i in 1..5 %}
{% set assigned_user = null %}
{% for user in users if user.planningday == i%}
{% set assigned_user = user %}
{% endfor %}
<td>
{% if not assigned_user is null %}
{{assigned_user.name}} {{assigned_user.lastname}}
{% endif %}
</td>
{% endfor %}
</tr>
<tr>
<th scope="row">Description</th>
{% for i in 1..5 %}
{% set assigned_user = null %}
{% for user in users if user.planningday == i%}
{% set assigned_user = user %}
{% endfor %}
<td>
{% if not assigned_user is null %}
{{assigned_user.planningcontent }}
{% endif %}
</td>
{% endfor %}
</tr>
</tbody>
责任的
{1..5%中的i为%1}
{%set assigned_user=null%}
{如果user.planningday==i%}
{%set assigned_user=user%}
{%endfor%}
{%如果未分配\用户为空%}
{{assigned_user.name}{{assigned_user.lastname}
{%endif%}
{%endfor%}
描述
{1..5%中的i为%1}
{%set assigned_user=null%}
{如果user.planningday==i%}
{%set assigned_user=user%}
{%endfor%}
{%如果未分配\用户为空%}
{{assigned_user.planningcontent}
{%endif%}
{%endfor%}
但我有符合条件的用户,并且我的上一个项目中使用了相同的细枝循环。我不明白为什么这里不起作用$users变量的内容是什么?你的循环没有多大意义。为什么
没有围绕for循环
和用户的显示在循环中完成?
{% for user in users if user.planningday == i%}
<tbody>
<tr>
<th scope="row">Responsable</th>
{% for i in 1..5 %}
{% set assigned_user = null %}
{% for user in users if user.planningday == i%}
{% set assigned_user = user %}
{% endfor %}
<td>
{% if not assigned_user is null %}
{{assigned_user.name}} {{assigned_user.lastname}}
{% endif %}
</td>
{% endfor %}
</tr>
<tr>
<th scope="row">Description</th>
{% for i in 1..5 %}
{% set assigned_user = null %}
{% for user in users if user.planningday == i%}
{% set assigned_user = user %}
{% endfor %}
<td>
{% if not assigned_user is null %}
{{assigned_user.planningcontent }}
{% endif %}
</td>
{% endfor %}
</tr>
</tbody>