Reputation: 167
I have an property of time type in my Entity
/**
* @ORM\Column(type="time")
*/
protected $max_time;
But in my twig template, it returns a object of DateTime type with an wrong value:
DateTime {#698 ▼
+"date": "1970-01-01 00:00:00"
+"timezone_type": 3
+"timezone": "America/Sao_Paulo"
}
In database the value of this data is 03:00:00
How can I fix it?
Upvotes: 2
Views: 1931
Reputation: 679
So I went and tried to replicate this to provide a valuable answer. However you have provided very limited information on how you got to this. What error you're receiving and so on... so I'm going to post what I did and it's results.
I've unfortunately found no issue. The error I got at first was it was unable to convert DateTime to string. Once I added the date
filter it stopped causing an error.
Entity
/**
* @var DateTime
*
* @ORM\Column(name="time", type="time")
*/
private $time;
Stored in Table
mysql> select * from Product;
+----+----------+-----------+-------+----------------+----------+
| id | owner_id | name | price | description | time |
+----+----------+-----------+-------+----------------+----------+
| 1 | 1 | Prod Name | 2.123 | WHat happened? | 11:21:00 |
+----+----------+-----------+-------+----------------+----------+
1 row in set (0.00 sec)
Twig Template
<table id="example">
<thead>
<th>Name</th>
<th>Description</ht>
<th>Price</th>
<th>Action</th>
</thead>
<tbody>
{% for product in products %}
<tr>
<td>{{ product.name }}</td>
<td>{{ product.description }}</td>
<td>{{ product.price }}</td>
<td>{{ product.time|date('H:i:s') }}{{ dump(product.time) }}</td>
<td>{% if is_granted('EDIT', product) %}
<a href="{{ path('app_product_show', {'id': product.id}) }}" class="btn btn-sm">Edit</a>
{% endif %}</td>
</tr>
{% endfor %}
</tbody>
</table>
Rendered
Upvotes: 1
Reputation: 2891
When you add annotation @ORM\Column(type="time")
to entity property in database (tested in MySQL) field of type "Time" will be created to store values like '00:00:00'. When doctrine fetch you entity it converts Time from database to PHP DateTime object reference
So to get time from this object just format it $entity->getTimeField()->format('H:i:s');
And don't forget that it should be real time value like '21:19:00' not some time lenght, ex '45:00:12'
Upvotes: 0