最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I convert an object to a string (Twig & Symfony)? - Stack Overflow

programmeradmin2浏览0评论

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 }}";

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 }}";

Share asked Feb 25, 2019 at 11:05 peace_lovepeace_love 6,47114 gold badges83 silver badges184 bronze badges 5
  • 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 Commented Feb 25, 2019 at 11:08
  • @Wulf This is clolumns_arr: array:4 [▼ 0 => "id" 1 => "name" 2 => "productgroup" 3 => "category.name" ] – peace_love Commented Feb 25, 2019 at 11:12
  • @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.name – DarkBee Commented Feb 25, 2019 at 11:28
  • @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/5O6mZP – peace_love Commented Feb 25, 2019 at 12:08
  • @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 fix – peace_love Commented Feb 25, 2019 at 12:24
Add a ment  | 

2 Answers 2

Reset to default 4

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.

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./use-jsonserializable-interface/

发布评论

评论列表(0)

  1. 暂无评论