【发布时间】:2013-07-23 08:29:56
【问题描述】:
我正在尝试使用 Twig 在我的页面上渲染一个数组,但我总是收到以下错误代码:
在渲染模板期间引发了异常(“注意:尝试获取 C:\xampp\htdocs\JPP\app\cache\dev\classes.php 第 10098 行中非对象的属性”) ProfileBundle:Profile:profile.html.twig 在第 228 行。
当我查看 profile.html.twig 时,我看到以下 for 循环:
`{% for Educations in Education|sortByDate %}
<div id="education{{loop.index0}}">
<tr>
<td class="border">{{ Educations.fromDate|date('d-m-Y') }}</td>
<td> </td>
<td class="border">{{ Educations.toDate|date('d-m-Y') }}</td>
<td> </td>
<td class="border">{{ Educations.company }}</td>
<td> </td>
<td class="border" id="jobsChar{{loop.index0}}">{{ Educations.jobs }}</td>
<td> </td>
<td class="border"><input type="button" title="Show Details" class="showDetails" id="details_show{{loop.index0}}"/><input type="button" title="Edit" class="editJob" id="education_edit{{loop.index0}}" /><form style="display:inline" action="../deleteJob" id="deleteJob"><input type="button" title="Delete Job" class="deleteJob" id="job_delete{{loop.index0}}" /></form></td>
</tr></div>{% endfor %}`
在下面你可以看到应该被TWIG的for循环处理的数组,但是没有成功......
array(5) { [0]=> array(9) { ["id"]=> int(80) ["userId"]=> int(3) ["company"]=> string(24 ) "Solothurner Spitäler AG" ["jobArea"]=> int(0) ["subJobsArea"]=> int(0) ["jobs"]=> int(2) ["jobLabel"]=> string(8) "Förster" ["fromDate"]=> object(DateTime)#437 (3) { ["date"]=> string(19) "2010-01-01 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/Berlin" } ["toDate"]=> object(DateTime)#436 (3) { ["date"]=> string(19) " 2011-02-09 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/Berlin" } } [1]=> array(9) { ["id"]=> int(87) ["userId"]=> int(3) ["company"]=> string(6) "TOP IT" ["jobArea"]=> int(0) [" subJobsArea"]=> int(0) ["jobs"]=> int(3) ["jobLabel"]=> string(20) "Dipl. Pflegefachfrau" ["fromDate"]=> object(DateTime)#433 ( 3) { ["date"]=> string(19) "2008-05-01 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "欧洲/Berlin" } ["toDate"]=> object(DateTime)#432 (3) { ["date"]=> string(19) "2009-05-22 00:00:00" ["timezone_type"]= > 整数(3) ["timezone"]=> string(13) "Europe/Berlin" } } [2]=> array(9) { ["id"]=> int(103) ["userId"]=> int(3) ["company"]=> string(10) "Helsana AG" ["jobArea"]=> int(0) ["subJobsArea"]=> int(0) ["jobs"]=> int(1) [" jobLabel"]=> string(11) "Holffäller" ["fromDate"]=> object(DateTime)#430 (3) { ["date"]=> string(19) "2012-10-01 00:00: 00" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/Berlin" } ["toDate"]=> object(DateTime)#429 (3) { ["date "]=> string(19) "2013-05-01 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/Berlin" } } [ 3]=> array(9) { ["id"]=> int(104) ["userId"]=> int(3) ["company"]=> string(8) "ALPIQ AG" ["jobArea" ]=> int(0) ["subJobsArea"]=> int(0) ["jobs"]=> int(2) ["jobLabel"]=> string(8) "Förster" ["fromDate"]=> object(DateTime)#427 (3) { ["date"]=> string(19) "2013-05-01 00:00:00" ["timezone_type"]=> int(3) ["timezone"]= > string(13) "Europe/Berlin" } ["toDate"]=> object(DateTime)#426 (3) { ["date"]=> string(19) "2013-05-09 00:00:00 " ["timezone_typ e"]=> int(3) ["timezone"]=> string(13) "Europe/Berlin" } } [4]=> array(9) { ["id"]=> int(130) [" userId"]=> int(3) ["company"]=> string(8) "ALPIQ AG" ["jobArea"]=> int(0) ["subJobsArea"]=> int(0) ["jobs" ]=> int(2) ["jobLabel"]=> string(8) "Förster" ["fromDate"]=> object(DateTime)#424 (3) { ["date"]=> string(19) " 2013-05-01 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/Berlin" } ["toDate"]=> object(DateTime) #439 (3) { ["date"]=> string(19) "2013-05-09 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(13 ) "欧洲/柏林" } } }
请问您能帮帮我吗?非常感谢。
罗杰干杯
【问题讨论】:
-
哪一行是 228 ?你在哪里转储数组?在控制器中还是在模板中?
-
错误可能出在您的 sortByDate 过滤器中。可以发一下代码吗?
-
@Mohammad: {% for Educations in Education|sortByDate %} 是第 228 行。抱歉,我忘了提及这些行!
-
@Webberig:我自己创建了 sortByDate 过滤器。这是代码:/** * *对数组进行排序。 * *@param array $a 一个数组 */ function twig_sortByDate_filter($a) { usort($a, "datumsvergleich");返回$a; } function datumsvergleich( $a, $b ) { if ( $a->fromDate == $b->fromDate ) 返回 0; if ( $a->fromDate fromDate ) 返回 1;返回-1; }
-
你能在 twig 函数的末尾(虽然在返回行之前)做 var_dump($a) 吗?