简体   繁体   English

Symfony2 / Twig-错误消息:注意:试图获取非对象的属性

[英]Symfony2 / Twig - Error Message: Notice: Trying to get property of non-object in

I am trying render an array on my page with Twig, but always I get the following error code: 我正在尝试使用Twig在页面上呈现数组,但始终会出现以下错误代码:

An exception has been thrown during the rendering of a template ("Notice: Trying to get property of non-object in C:\\xampp\\htdocs\\JPP\\app\\cache\\dev\\classes.php line 10098") in ProfileBundle:Profile:profile.html.twig at line 228. 在ProfileBundle:Profile中呈现模板时,抛出了一个异常(“注意:试图在C:\\ xampp \\ htdocs \\ JPP \\ app \\ cache \\ dev \\ classes.php行10098中获取非对象的属性”) :profile.html.twig在第228行。

When I look at profile.html.twig i see the following for-loop: 当我查看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 %}` 

In the following you can see the array which should be processed by the for-loop of TWIG, but without success... 在下面的内容中,您可以看到应该由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) "Europe/Berlin" } ["toDate"]=> object(DateTime)#432 (3) { ["date"]=> string(19) "2009-05-22 00:00:00" ["timezone_type"]=> int(3) [ array(5){[0] => array(9){[“” id“] => int(80)[” userId“] => int(3)[” company“] =>字符串(24)” Solothurner SpitälerAG“ [” 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”] =>字符串(13)“欧洲/柏林”} [“ toDate”] => object(DateTime)#436(3){[“” date“] =>字符串(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“] =>字符串(6)” TOP IT“ [” jobArea“] => int(0)[” subJobsArea“] => int(0)[“ jobs”] => int(3)[“ jobLabel”] =>字符串(20)“ Dipl。Pflegefachfrau” [“ fromDate”] => object(DateTime)#433(3){ [“ date”] =>字符串(19)“ 2008-05-01 00:00:00” [“ timezone_type”] => int(3)[“ timezone”] =>字符串(13)“欧洲/柏林” } [“ toDate”] => object(DateTime)#432(3){[“ date”] => string(19)“ 2009-05-22 00:00:00” [“ timezone_type”] => int( 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_ty “ timezone”] =>字符串(13)“欧洲/柏林”}} [2] => array(9){[“” id“] => int(103)[” userId“] => int(3)[ “ company”] =>字符串(10)“ Helsana AG” [“ jobArea”] => int(0)[“ subJobsArea”] => int(0)[“ jobs”] => int(1)[“ jobLabel “] =>字符串(11)”Holffäller“ [” fromDate“] => object(DateTime)#430(3){[”“ date”] =>字符串(19)“ 2012-10-01 00:00:00 “ [[timezone_type”] => int(3)[“ timezone”] =>字符串(13)“欧洲/柏林”} [“ toDate”] => object(DateTime)#429(3){[“” ] => 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”] =>字符串(8)“ ALPIQ AG” [“ jobArea”] => int(0)[“ subJobsArea”] => int(0)[“ jobs”] => int(2)[“ jobLabel”] => string(8)“Förster” [“ fromDate”] =>对象(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_ty pe"]=> 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) "Europe/Berlin" } } } pe“] => int(3)[” timezone“] => string(13)”欧洲/柏林“}} [4] => array(9){[” id“] => int(130)[” userId“] => int(3)[” company“] => string(8)” ALPIQ AG“ [” jobArea“] => int(0)[” subJobsArea“] => int(0)[” jobs“ ] => int(2)[“ jobLabel”] =>字符串(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”] =>字符串(19)“ 2013-05-09 00:00:00” [“ timezone_type”] => int(3)[“ timezone”] =>字符串(13 )“欧洲/柏林”}}}

Please can you help me? 请你能帮我吗? Thank you very much. 非常感谢你。

Cheers Roger 干杯罗杰

By default, Twig will try to escape returned data from filters. 默认情况下,Twig将尝试转义过滤器返回的数据。 It's possible that Twig doesn't expect the return result to be an array and break it. Twig可能不希望返回结果是一个数组并将其破坏。 Try setting "is_safe" to true when defining your Twig filter. 定义Twig过滤器时,请尝试将“ is_safe”设置为true。

If that doesn't work, I doubt you can use a filter like that. 如果这不起作用,我怀疑您可以使用类似的过滤器。 Filters are generally used for text manipulation, not complex data. 过滤器通常用于文本操作,而不是复杂的数据。 You could try definining a function instead of a filter, or in worst case process your data where you should => the controller. 您可以尝试定义一个函数而不是一个过滤器,或者在最坏的情况下,应在应该=>控制器的地方处理数据。

Your problem isn't in twig, add a line in the beginning {{ dump(Educations) }} and I'm sure it will return null. 您的问题不在枝上,请在{{ dump(Educations) }}开头添加一行,我确定它将返回null。 The problem is in the controller, you said you dump it and it worked, but I'm assuming you dumped it in the controller, can you please add the action code or the last few lines of it so see how exactly you are passing the variable to the template. 问题出在控制器上,您说过将其转储并且可以正常工作,但是我假设您将其转储到控制器中,能否请您添加操作代码或代码的最后几行,以便了解如何准确地传递模板的变量。

您可以尝试删除“ | sortByDate”吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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