【问题标题】:Symfony2 / Twig - Error Message: Notice: Trying to get property of non-object inSymfony2 / Twig - 错误消息:注意:试图获取非对象的属性
【发布时间】: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>&nbsp;</td>
                                <td class="border">{{ Educations.toDate|date('d-m-Y') }}</td>
                                <td>&nbsp;</td>                                    
                                <td class="border">{{ Educations.company }}</td>
                                <td>&nbsp;</td>
                                <td class="border" id="jobsChar{{loop.index0}}">{{ Educations.jobs }}</td>
                                <td>&nbsp;</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) 吗?

标签: symfony twig


【解决方案1】:

默认情况下,Twig 会尝试从过滤器中转义返回的数据。 Twig 可能不希望返回结果是一个数组并破坏它。在定义 Twig 过滤器时尝试将“is_safe”设置为 true。

如果这不起作用,我怀疑你可以使用这样的过滤器。过滤器通常用于文本操作,而不是复杂数据。您可以尝试定义一个函数而不是过滤器,或者在最坏的情况下处理您的数据,您应该 => 控制器。

【讨论】:

    【解决方案2】:

    你的问题不在树枝上,在开头添加一行{{ dump(Educations) }},我相信它会返回 null。 问题出在控制器中,你说你转储它并且它工作,但我假设你把它转储到控制器中,你能添加操作代码或它的最后几行,看看你是如何通过的模板的变量。

    【讨论】:

      【解决方案3】:

      你能尝试删除“|sortByDate”吗

      【讨论】:

        猜你喜欢
        • 2014-05-03
        • 1970-01-01
        • 2018-11-22
        • 2014-10-01
        相关资源
        最近更新 更多