Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 多个关系:数据已正确保存但无法显示:类别为空?_Php_Database_Symfony_Twig - Fatal编程技术网

Php 多个关系:数据已正确保存但无法显示:类别为空?

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

在多个关系(广告和类别)中,数据库中充满了数据,但当我尝试在Twig中显示结果时,出现了以下错误:

致命错误:无法访问空属性

班级广告的这一行上

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>