2

I don't know what is going on but here is my predicament:
When I want to access nested arrays in twig I get this error

Impossible to access an attribute ("path") on a integer variable ("10977") in KprCentarZdravljaBundle:Parts:articleDefaultBlock.html.twig at line 12

Now here is my twig code:

{% if flag == 0 or flag == 1 %}
    {% set width = 230 %}
    {% set height = 154 %}
{% elseif flag == 2 %}
    {% set width = 60 %}
    {% set height = 60 %}
{% endif %}

{% for artCats in articles %}
  {% for artCat in artCats %}
    {% set img_url = globalimagepath ~ "articles/" ~ artCat.path %}
    {% set cid = artCat.cid %}
    {% set catname = artCat.name %}
    {% set aPath = path('article_details', {'id': artCat.id, 'catname': catname|pathauto,  'title': artCat.title|pathauto}) %}
     <li>
        <article>
            {% if loop.first %}
            {% if flag == 0 or flag == 1 %}
              <h3><a href="{{ path('category_details', {'id': cid, 'name': catname|pathauto}) }}">{{ catname|teaser(28) }}</a></h3>
            {% endif %}
            {% if flag != 3 %}<a href="{{ aPath }}"><img src="{{ image('http://'~ app.request.getHttpHost() ~ asset(img_url)).zoomCrop(width,height).jpeg }}" alt="{{ artCat.title }}"></a>{% endif %}
                <h1><a href="{{ aPath }}">{{ artCat.title|teaser(79) }}</a></h1>
            {% else %}
                <h2><a href="{{ path('article_details', {'id': artCat.id, 'catname': artCat.name|pathauto,  'title': artCat.title|pathauto}) }}">{{ artCat.title|teaser(37) }}</a></h2>
            {% endif %}
            </article>
      </li>
  {% endfor %}
{% endfor %}

And here is my debug of the variable articles in twig:

array(4) {
  ["Zdravlje općenito"]=&gt;
  array(11) {
    [0]=&gt;
    array(5) {
      ["id"]=&gt;
      int(11659)
      ["title"]=&gt;
      string(36) "Brinete li o zdravlju svojih očiju?"
      ["path"]=&gt;
      string(18) "1330007101_516.jpg"
      ["cid"]=&gt;
      int(266)
      ["name"]=&gt;
      string(18) "Zdravlje općenito"
    }
    [1]=&gt;
    array(5) {
      ["id"]=&gt;
      int(14663)
      ["title"]=&gt;
      string(29) "Otkrijte uzročnike alergija!"
      ["path"]=&gt;
      string(18) "1380196476_342.jpg"
      ["cid"]=&gt;
      int(266)
      ["name"]=&gt;
      string(18) "Zdravlje općenito"
    }
    [2]=&gt;
    array(5) {
      ["id"]=&gt;
      int(14664)
      ["title"]=&gt;
      string(24) "Uzroci bolova u kukovima"
      ["path"]=&gt;
      string(18) "1380195386_853.jpg"
      ["cid"]=&gt;
      int(266)
      ["name"]=&gt;
      string(18) "Zdravlje općenito"
    }
    [3]=&gt;
    array(5) {
      ["id"]=&gt;
      int(14665)
      ["title"]=&gt;
      string(13) "Napadi panike"
      ["path"]=&gt;
      string(18) "1380195261_180.jpg"
      ["cid"]=&gt;
      int(266)
      ["name"]=&gt;
      string(18) "Zdravlje općenito"
    }
    [4]=&gt;
    array(5) {
      ["id"]=&gt;
      int(11170)
      ["title"]=&gt;
      string(28) "Čaj od ružmarina kao lijek"
      ["path"]=&gt;
      string(18) "1380116708_577.jpg"
      ["cid"]=&gt;
      int(266)
      ["name"]=&gt;
      string(18) "Zdravlje općenito"
    }
    [5]=&gt;
    array(5) {
      ["id"]=&gt;
      int(10970)
      ["title"]=&gt;
      string(35) "Začinite i produžite svoj život!"
      ["path"]=&gt;
      string(18) "1380031350_101.jpg"
      ["cid"]=&gt;
      int(266)
      ["name"]=&gt;
      string(18) "Zdravlje općenito"
    }
    [6]=&gt;
    array(5) {
      ["id"]=&gt;
      int(11171)
      ["title"]=&gt;
      string(32) "Meditacijom do punine življenja"
      ["path"]=&gt;
      string(18) "1380103969_112.jpg"
      ["cid"]=&gt;
      int(266)
      ["name"]=&gt;
      string(18) "Zdravlje općenito"
    }
    [7]=&gt;
    array(5) {
      ["id"]=&gt;
      int(10973)
      ["title"]=&gt;
      string(20) "Zašto volimo cimet?"
      ["path"]=&gt;
      string(18) "1379579290_899.jpg"
      ["cid"]=&gt;
      int(266)
      ["name"]=&gt;
      string(18) "Zdravlje općenito"
    }
    [8]=&gt;
    array(5) {
      ["id"]=&gt;
      int(10977)
      ["title"]=&gt;
      string(32) "Simptomi hormonalne neravnoteže"
      ["path"]=&gt;
      string(18) "1379507943_927.jpg"
      ["cid"]=&gt;
      int(266)
      ["name"]=&gt;
      string(18) "Zdravlje općenito"
    }
    [9]=&gt;
    array(5) {
      ["id"]=&gt;
      int(10978)
      ["title"]=&gt;
      string(46) "Čaj od đumbira - ukusni napitak koji liječi"
      ["path"]=&gt;
      string(18) "1379419241_320.jpg"
      ["cid"]=&gt;
      int(266)
      ["name"]=&gt;
      string(18) "Zdravlje općenito"
    }
    [10]=&gt;
    array(5) {
      ["id"]=&gt;
      int(10979)
      ["title"]=&gt;
      string(24) "Zašto volimo ružmarin?"
      ["path"]=&gt;
      string(18) "1379335723_831.jpg"
      ["cid"]=&gt;
      int(266)
      ["name"]=&gt;
      string(18) "Zdravlje općenito"
    }
  }
  ["Izgled i ljepota"]=&gt;
  array(3) {
    [0]=&gt;
    array(5) {
      ["id"]=&gt;
      int(14662)
      ["title"]=&gt;
      string(20) "Joga za mršavljenje"
      ["path"]=&gt;
      string(18) "1380198181_298.jpg"
      ["cid"]=&gt;
      int(268)
      ["name"]=&gt;
      string(16) "Izgled i ljepota"
    }
    [1]=&gt;
    array(5) {
      ["id"]=&gt;
      int(10971)
      ["title"]=&gt;
      string(32) "Najbolja prehrana za vašu kožu"
      ["path"]=&gt;
      string(18) "1379939148_517.jpg"
      ["cid"]=&gt;
      int(268)
      ["name"]=&gt;
      string(16) "Izgled i ljepota"
    }
    [2]=&gt;
    array(5) {
      ["id"]=&gt;
      int(10974)
      ["title"]=&gt;
      string(30) "Najčešći uzročnici rosacee"
      ["path"]=&gt;
      string(18) "1379579025_722.jpg"
      ["cid"]=&gt;
      int(268)
      ["name"]=&gt;
      string(16) "Izgled i ljepota"
    }
  }
  ["Seks i veze"]=&gt;
  array(1) {
    [0]=&gt;
    array(5) {
      ["id"]=&gt;
      int(10975)
      ["title"]=&gt;
      string(19) "10 mitova o orgazmu"
      ["path"]=&gt;
      string(18) "1379578683_758.jpg"
      ["cid"]=&gt;
      int(270)
      ["name"]=&gt;
      string(11) "Seks i veze"
    }
  }
  ["Sport i rekreacija"]=&gt;
  array(1) {
    [0]=&gt;
    array(5) {
      ["id"]=&gt;
      int(10976)
      ["title"]=&gt;
      string(41) "Što se događa s tijelom kada vježbate?"
      ["path"]=&gt;
      string(18) "1379577938_298.jpg"
      ["cid"]=&gt;
      int(271)
      ["name"]=&gt;
      string(18) "Sport i rekreacija"
    }
  }
}

For some reason I can't access any of the variables in the nested array...

2 Answers 2

2

Impossible to access an attribute ("path") on a integer variable ("10977") in KprCentarZdravljaBundle:Parts:articleDefaultBlock.html.twig at line 12

Do you get the error only with the 8th element in the array (the one with id 10977)? Do the iterations work before reaching that element ?

Sign up to request clarification or add additional context in comments.

1 Comment

Ok sry to all the people because I just figured out I'm semi retarded :(. I used the same template for different types of results on the same index page. Actually everything was ok with this part I posted in the question, the error was in another block on my index site that used the same template but passed a normal array not a nested one :(. Sry for wasting everybody's time.
0

It seems like this is failing: {% set img_url = globalimagepath ~ "articles/" ~ artCat.path %}

Do {{ dump(artCat) }}. It seems to be an integer instead of array/object.

4 Comments

I posted the dump(articles). The artCat is inside the articles array. The problem is not in the path variable, if I put artCat.id I still get the same error but insted Impossible to access an attribute ("path") i get Impossible to access an attribute ("id")
Did you try dumping that artCat inside loop ?
Yes, all of the data is there here is one artCat: array(5) { ["id"]=&gt; int(11659) ["title"]=&gt; string(36) "Brinete li o zdravlju svojih očiju?" ["path"]=&gt; string(18) "1330007101_516.jpg" ["cid"]=&gt; int(266) ["name"]=&gt; string(18) "Zdravlje općenito" }
This is even funnier, when I put artCat[0] instead artCat.path I get this error: Key "0" for array with keys "id, title, path, cid, name" does not exist in KprCentarZdravljaBundle:Parts:articleDefaultBlock.html.twig at line 13 - So funny because the system knows its an array and what are the array keys, but when i try to access one I cant :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.