Nico.Lbrgn
Nico.Lbrgn

Reputation: 13

[Symfony][Twig] - Twig display a null value while dumping the object shows a not null value

I'm on a symphony project, in development environement. I'm trying to display an object, inside a twig view. My controller get the object by querying an entity repository, and give it to the view.

This is what my object looks like when I dump it into the view with the twig {{ dump(article) }} function :

Article {#983 ▼
  -id: 1
  -createDate: DateTime {#1155 ▼
    +"date": "2015-12-21 23:31:11.000000"
    +"timezone_type": 3
    +"timezone": "Europe/Berlin"
  }
  -updateDate: DateTime {#1063 ▼
    +"date": "2016-06-13 13:30:31.000000"
    +"timezone_type": 3
    +"timezone": "Europe/Berlin"
  }
  -author: "author"
  -title: "A good title"
  -content: "<p>For a good content</p>"
}

I can access to all the value inside the object, except the "updateDate". If I {{ dump(article.createDate) }}, I get this :

DateTime {#1160 ▼
  +"date": "2015-12-21 23:31:11.000000"
  +"timezone_type": 3
  +"timezone": "Europe/Berlin"
}

and if I {{ dump(article.updateDate) }} I get this :

null

While the full object dump give me something similar than article.createDate for article.updateDate.

How can I fix that ? I need to use my article.updateDate in my twig view.

Thanks

Upvotes: 1

Views: 1198

Answers (2)

Nico.Lbrgn
Nico.Lbrgn

Reputation: 13

Thanks Ignas,

I tried with the getter and it worked good. I did not know that I can use getter in the view, but it's so logical ...

I think I will use only getters in the view now, I found this more logic as my entity attributes are defined in private ...

Thanks all for your help !

Upvotes: 0

Ignas Damunskis
Ignas Damunskis

Reputation: 1504

Okay try few things:

  1. {{ updateDate }} if you pass Article object as params array from controller
  2. {{ article.getUpdateDate }} to access parameter through getter

Upvotes: 2

Related Questions