Php 多个关系:数据已正确保存但无法显示:类别为空?
在多个关系(广告和类别)中,数据库中充满了数据,但当我尝试在Twig中显示结果时,出现了以下错误: 致命错误:无法访问空属性 在Php 多个关系:数据已正确保存但无法显示:类别为空?,php,database,symfony,twig,Php,Database,Symfony,Twig,在多个关系(广告和类别)中,数据库中充满了数据,但当我尝试在Twig中显示结果时,出现了以下错误: 致命错误:无法访问空属性 在班级广告的这一行上: public function getCategories(){ return $this->$categories; } 这是细枝页: <p> {% if not advert.categories.empty %} {% for cat in advert.categories %} {{ cat.na
班级广告的这一行上
:
public function getCategories(){
return $this->$categories;
}
这是细枝页:
<p>
{% if not advert.categories.empty %}
{% for cat in advert.categories %}
{{ cat.name }}{% if not loop.last %}, {% endif %}
{% endfor %}
{% endif %}
</p>
谢谢您必须使用
$this->categories
而不是$this->$categories
。后者将首先解析变量$categories
,然后尝试使用该值的名称访问属性,而不是访问属性category您必须使用$this->categories
而不是$this->$categories
。后者将首先解析变量$categories
,然后尝试访问具有该值名称的属性,而不是访问控制器更改中的属性category
$this->$categories
到
在你的树枝上:
<p>
{% for cat in advert.categories %}
{{ cat.name }}
{% else %}
No categories found...
{% endfor %}
</p>
{广告类别中猫的百分比%}
{{cat.name}
{%else%}
找不到类别。。。
{%endfor%}
在控制器中更改
$this->$categories
到
在你的树枝上:
<p>
{% for cat in advert.categories %}
{{ cat.name }}
{% else %}
No categories found...
{% endfor %}
</p>
{广告类别中猫的百分比%}
{{cat.name}
{%else%}
找不到类别。。。
{%endfor%}
不应该是$this->categories代码>而不是$this->$categories代码>?@IhorBurlachenko非常感谢,它解决了它。它不应该是$this->categories代码>而不是$this->$categories代码>?@IhorBurlachenko非常感谢,它解决了这个问题。
<p>
{% for cat in advert.categories %}
{{ cat.name }}
{% else %}
No categories found...
{% endfor %}
</p>