1

i have a little problem and I really don't know why. I am printing a datetime variable in twig with |date() but it is allways printing the actual time.

for Debugging I put the following Code in my Template:

<pre>
{% debug entity.getCreatedAt|date("d.m.Y H:i:s") %}
{% debug entity.getCreatedAt|raw %}
{% debug entity.CreatedAt|raw %}
{% debug entity.CreatedAt|date("d.m.Y H:i:s") %}

My Variable is called CreatedAt so normally I should get the correct output with entity.CreatedAt|date("d.m.Y H:i:s"), right?

My Debug output is as follows:

string '16.01.2013 13:46:03' (length=19) //entity.getCreatedAt|date("d.m.Y H:i:s")

object(DateTime)[4611] //entity.getCreatedAt|raw
 public 'date' => string '2013-01-16 13:46:03' (length=19)
 public 'timezone_type' => int 3
 public 'timezone' => string 'Europe/Berlin' (length=13)

object(DateTime)[4938] //entity.CreatedAt|raw
 public 'date' => string '2013-02-20 21:46:53' (length=19)
 public 'timezone_type' => int 3
 public 'timezone' => string 'Europe/Berlin' (length=13)

string '20.02.2013 21:46:53' (length=19) //entity.CreatedAt|date("d.m.Y H:i:s")

I don't understand why it is NULL as soon as I call CreatedAt. And OUTSIDE of the debug tag it is ALLWAYS NULL, not depending on the writing.

In my Entity I've got:

private $CreatedAt;

public function setCreatedAt($createdAt)
{
    $this->CreatedAt = $createdAt;

    return $this;
}

public function getCreatedAt()
{
    return $this->CreatedAt;
}

And in the YML I've got:

CreatedAt:
  type: datetime
  nullable: true

Does anybody see a mistake?? I really don't find it, maybe it is a bug?

Thanks

Tny
  • 165
  • 4
  • 11
  • Do you have the same result using `{{ entity.CreatedAt|date("d.m.Y H:i:s") }}`? – cheesemacfly Feb 20 '13 at 21:02
  • I have tested and really can't reproduce...Do you get the same result using [dump](http://twig.sensiolabs.org/doc/functions/dump.html) instead of `{% debug %}? – cheesemacfly Feb 21 '13 at 20:55
  • 1
    Hello Cheesemacfly, i solved my problem today - and it was a really stupid mistake! I had a prePersist Class called CreatedAt() and Twig was of course calling it - and recieving NULL.. so i renamed my prePersist class to prePersist() and now it is working perfectly! – Tny Feb 28 '13 at 12:01

2 Answers2

4

If you want to access your private variable $CreatedAt outside your entity class, you have to call the public getter method getCreatedAt() (that's what it's here for).

And in your twig template, when you call {% debug entity.CreatedAt|date("d.m.Y H:i:s") %}, since entity.CreatedAt is NULL, the returned string is based on a new date object:

If the value passed to the date filter is null, it will return the current date by default.

http://twig.sensiolabs.org/doc/filters/date.html

UPDATE:

As mentioned by @insertusernamehere, twig automatically calls the public getter.
But this behavior seems to happen only when using the delimiters {{ }} over {% %}.

http://twig.sensiolabs.org/doc/templates.html#synopsis

cheesemacfly
  • 11,622
  • 11
  • 53
  • 72
  • Actually Twig will call the getter method for you. – insertusernamehere Feb 20 '13 at 20:40
  • Thank you for your answer, i also tried it with public $createdAt, but it doesn't change this behaviour. And the most interesting thing I think is that in {% debug entity.getcreatedat|date("d.m.Y H:i:s") %} I get the right result, but without debug I get NULL... that is what I really don't understand – Tny Feb 20 '13 at 20:43
  • @insertusernamehere At first I agreed with you but are you sure that this is the case when the object is access between `{% %}` instead of `{{ }}`? – cheesemacfly Feb 20 '13 at 21:01
  • It seems you've been right. Added public to CreatedAt and print it with entity.CreatedAt|date("d.m.Y H:i:s") and it is working now. But for Example I also got a string "private $PersonGender;" and this one is also printing in private mode, only with the datetime I needed the public. Maybe that is because it needs to do some ->format() work on the public method? – Tny Feb 20 '13 at 21:06
  • If you use `{{ entity.CreatedAt|date("d.m.Y H:i:s") }}` don't you get the expected result? – cheesemacfly Feb 20 '13 at 21:07
  • Yes, but only as long as it is public. All other variables used are still private and are working, only needed to change all DateTime variables. – Tny Feb 20 '13 at 21:09
  • This is really weird because as @insertusernamehere said, Twig calls the getter for you... – cheesemacfly Feb 20 '13 at 21:10
  • So maybe it is really a bug? – Tny Feb 20 '13 at 21:11
  • Sorry guys I was away a few minutes - what's the conclusion? – insertusernamehere Feb 20 '13 at 21:12
  • If i turn CreatedAt to public and call {{ entity.CreatedAt|date("d.m.Y H:i:s") }} it is working, little bit strange I think, but fine :-) – Tny Feb 20 '13 at 21:13
  • @insertusernamehere my conclusion was you were right but it is the case only when using `{{ }}` and not `{% %}` but I can't find anything in the documentation about this... – cheesemacfly Feb 20 '13 at 21:13
  • I have updated my answer but I will run some tests later to confirm this is true! But still strange :) – cheesemacfly Feb 20 '13 at 21:19
  • But @cheesemacfly I only used the {% %} tags for demonstration, now I am using {{ }} and still need the public – Tny Feb 20 '13 at 21:26
  • @Tny definitely weird...I will run some tests later (probably tomorrow) and see if I can reproduce. – cheesemacfly Feb 20 '13 at 21:28
2

In PHP member functions are case insensitive and class members are case sensitive. So getCreatedAt will work as good as getcreatedat but CreatedAt differs from createdat.

Here are some further informations about that: Why are functions and methods in PHP case-insensitive?.

Community
  • 1
  • 1
insertusernamehere
  • 23,204
  • 9
  • 87
  • 126
  • Hello insertusernamehere, yes I know about case-sensitive, just did this for showing my error. Please have a look at the example above, i will update it in a second. – Tny Feb 20 '13 at 20:48