且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 symfony 3 中将对象数组转换为字符串?

更新时间:2022-06-17 04:51:10

当您自动创建 CRUD 时,此问题很常见.

This problem is common when you've created the CRUD automatically.

问题是你需要从一个中选择一个Product in the Offer form,symfony无法绘制select,因为Product类未指定应呈现哪个字段.

The problem is that you need to select from a <select> a Product in the Offer form and symfony cannot draw the select because the Product class doesn't specify which field should be rendered.

转到您的产品实体并添加神奇的 __toString 方法(如果可以,请提供它),它应该如下所示:

Go to your Product Entity and add the magic __toString method (provide it if you can) and it should look like :

class Product {

    public function __toString(){
        // Or change the property that you want to show in the select.
        return $this->name;
    }
}