0
votes

I have a jquery function that is mixed with twig data:

$(document).on('change', '.item-select', function() {

var optionValue = $(this).val();
{% for key, value in columns_arr %}
{% for k,v in group %}
if (optionValue == "{{ v.id }}") {
  {% set output = v %}
  {% for method in value|split('.') if method != '' %}
  {% set output = attribute(output, method) | default('') %}
  {% endfor %}
  var {{ value | split('.') | first }} = "{{ output }}";
}
{% endfor %}
{% endfor %}


if (optionValue) {
  var entity = $(this).find(':selected').attr('data-parent');
  var relation = $(this).find(':selected').attr('data-slug');
  var uuid= $(this).find(':selected').attr('data-id');


  table.row.add({
    {% for key, value in columns_arr %}
    {% for k,v in group %}
    "{{ value | split('.') | first }}": {{ value | split('.') | first }},
    {% endfor %}
    {% endfor %}
  }).draw();
  $('option', this).first().prop('selected', true);
  fetch(`/row/${entity}/${relation}/${uuid}/${optionValue}`,{
    method: 'POST'
  }).then(res => window.location.reload());
}

});

I get the error message:

An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class Proxies__CG__\App\Entity\Productgroup could not be converted to string").

And the error should be in this line:

var {{ value | split('.') | first }} = "{{ output }}";

2
Can you give us an idea, what column_arr looks like? It seems it is an array of Productgroup entities. You should call the specific field of them ({{ value.name | split('.') | first }} for example) or give these entities a __toString method.Wulf
@Wulf This is clolumns_arr: array:4 [▼ 0 => "id" 1 => "name" 2 => "productgroup" 3 => "category.name" ]peace_love
@Jarla - What is your expected output? Don't forget you can easily access any property for the object productgroup by changing the column definition. e.g productgroup.nameDarkBee
@DarkBee To demonstrate to you, here are two variations of the arrays. The first variation is working and the second variation getting the error. I need the second variation to work too: codeshare.io/5O6mZPpeace_love
@DarkBee I think I know what is the problem. In the example code I was sending you I see that colums_arr should be array:4 [▼ 0 => "id" 1 => "name" 2 => "productgroup.name" 3 => "category.name" ] instead of array:4 [▼ 0 => "id" 1 => "name" 2 => "productgroup" 3 => "category.name" ] I think this is where I need to make the fixpeace_love

2 Answers

2
votes

Maybe you could try implementing JsonSerializable in your Entity to then use its methods __toString() and jsonSerialize and rewrite them as u want.

https://www.sitepoint.com/use-jsonserializable-interface/

3
votes

If you give Twig an object, it implicitly calls the __toString() method on that object. That is how you get the error message.

Are you looking for a variable value on that object? In such case, use the field name (e.g. output.something).

What you are apparently trying to do is use the object as an object and handle it with javascript functions. The easiest way to do that is typically to use the json_encode filter, which will produce a JSON object with proper encoding and everything, provided your underlying Symfony/Doctrine object is clean.

var {{ value | split('.') | first }} = "{{ output | json_encode }}";

should do the trick.

But honestly, I think that style of code is asking for trouble. You should assign your variables explicitly and not iterate over field names the way you seem to be doing.