0

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

可捕获的致命错误:FoodBundle\Entity\Product 类的对象无法转换为字符串

其中 Product 实体与 Offer 实体相关。我的目标是利用多对多关系的产品报价。

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

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

请帮我解决这个问题。

4

1 回答 1

0

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

问题是您需要从Offer 表单中<select>的Product 中进行选择,而 symfony 无法绘制选择,因为Product 类没有指定应该呈现哪个字段

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

class Product {

    public function __toString(){
        // Or change the property that you want to show in the select.
        return $this->name;
    }
}
于 2016-07-29T12:56:02.520 回答