Reputation: 1558
I have an Entity called Keyword, and it has two rows:
id comment_id foo_id text
2 1 1 Jajajaja :) Hola.
3 2 1 Chao
foo_id and comment_id are foreign keys and integer values. Foo_id from table Foo and comment_id from table Comments.
I'm trying to print comment_id and foo_id in a twig template.
{% for k in keywords %}
{{ k.id}} , {{ k.text}}, {{ k.comment }}, {{ k.foo}}
{% else %}
but it gives me this error:
An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class
Proxies\PruebasRBundleEntityCommentsProxy could not be
converted to string in C:\wamp\www\sym\app\cache\dev\twig\52\c9\3138bf2dc905760b186f2d006484.php
line 74") in PruebasRBundle:Default:keywords.html.twig at line 24.
So, I tried printing the values that are not Foreign keys
I'm trying to print comment_id and foo_id in a twig template.
{% for k in keywords %}
{{ k.id}} , {{ k.text}} <!--No k.comment no k.foo -->
{% else %}
it works this way, but I can't print foreign keys values and I need them.
It makes me think that there's something with the relationships :(.
Upvotes: 0
Views: 2221
Reputation: 8915
That's because Doctrine automatically detects the k.comment foreign key, and returns you an object representing the foreign comment row.
This object is a PruebasRBundle\Entity\Comments instance. (Doctrine2 will wrap it in a proxy to ease lazy loading).
Your problem is that you try to cast this object as a string.
Etiher modify your twig like this:
{% for k in keywords %}
{{ k.id}} , {{ k.text}}, {{ k.comment.id }}, {{ k.foo}}
{% else %}
Or implement the __toString
method in your Comment
class.
Upvotes: 2
Reputation: 2597
If you want to print the numeric values try this way:
{% for k in keywords %}
{{ k.id}} , {{ k.text}}, {{ k.comment_id }}, {{ k.foo_id}}
{% else %}
If you want to print some value from related entities try creating a magic method __toString() for entities Foo and Comments. Then:
{% for k in keywords %}
{{ k.id}} , {{ k.text}}, {{ k.comments }}, {{ k.foo}}
{% else %}
Upvotes: 0