中将对象数组转换为字符串

中将对象数组转换为字符串

本文介绍了如何在 symfony 3 中将对象数组转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 b/w 2 实体(产品和报价)中实现多对多关系时出现以下错误:

I'm getting the following error while implementing the many-to-many relationship in b/w 2 entities(Product & Offer):

可捕获的致命错误:FoodBundle\Entity\Product 类的对象可以不会被转换成字符串

产品实体与要约实体的关系.我的目标是利用多对多关系的产品报价.

Where Product entity is in relation with the Offer entity.My aim here was to avail offers to products in many-to-many relationship.

这段代码正在创建它,正如我从错误中猜测的那样.

This piece of code is creating it as I guess from the error.

class Offer
{
    public function addProduct(\FoodBundle\Entity\Product $product)
    {
        $this->product[] = $product;
        return $this;
    }
}

请帮我解决这个问题.

推荐答案

当您自动创建 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;
    }
}

这篇关于如何在 symfony 3 中将对象数组转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 14:13