简体   繁体   English

如何将对象转换为字符串(Twig和Symfony)?

[英]How can I convert an object to a string (Twig & Symfony)?

I have a jquery function that is mixed with twig data: 我有一个与树枝数据混合的jquery函数:

$(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"). 呈现模板期间引发了异常(“可捕获的致命错误:Proxies__CG __ \\ App \\ Entity \\ Productgroup类的对象无法转换为字符串”)。

And the error should be in this line: 错误应该在这一行:

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

Maybe you could try implementing JsonSerializable in your Entity to then use its methods __toString() and jsonSerialize and rewrite them as u want. 也许您可以尝试在Entity中实现JsonSerializable ,然后使用其方法__toString()jsonSerialize并根据需要重写它们。

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

If you give Twig an object, it implicitly calls the __toString() method on that object. 如果给Twig一个对象,它将隐式调用该对象的__toString()方法。 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 (eg output.something). 在这种情况下,请使用字段名称(例如output.something)。

What you are apparently trying to do is use the object as an object and handle it with javascript functions. 您显然想做的是将对象用作对象,并使用javascript函数处理它。 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. 最简单的方法通常是使用json_encode过滤器,只要您的基础Symfony / Doctrine对象是干净的,它就会生成具有正确编码和所有内容的JSON对象。

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. 您应该显式分配变量,而不要像看起来那样遍历字段名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM