BackendBundleEntityCategoria 类的对象无法转换为字符串转换为、字符串、对象、BackendBundleEntityCategoria

2023-09-07 00:44:10 作者:浪得一身野气

我创建了一个包含产品和类别的数据库:

I have created a database with products and categories:

CREATE TABLE categoria(
id       int(255) auto_increment not null,
nombre     varchar(50),
CONSTRAINT pk_categoria PRIMARY KEY(id),
)ENGINE = InnoDb;

CREATE TABLE producto(
id       int(255) auto_increment not null,
nombre     varchar(50),
categoria int(11),
createdAt datetime,
updatedAt datetime,
CONSTRAINT pk_producto PRIMARY KEY(id),
CONSTRAINT fk_producto_categoria FOREIGN KEY(categoria) references categoria(id),
)ENGINE = InnoDb;

当使用 twig 打印产品时,它会显示除类别编号之外的所有字段,我收到错误消息:

When printing with twig the product, and it shows all the fields, except the one of the category number, that I get the error:

在渲染模板期间引发了异常(Catchable >Fatal Error: Object of class Proxies__CG__BackendBundleEntityCategoria >无法转换为字符串").

An exception has been thrown during the rendering of a template ("Catchable >Fatal Error: Object of class Proxies__CG__BackendBundleEntityCategoria >could not be converted to string").

有解决这个问题的办法吗?

Any idea to solved this issue?

BackendBundleEntityProducto:
    type: entity
    table: producto
    indexes:
        fk_producto_categoria:
            columns:
                - categoria
    id:
        id:
            type: integer
            nullable: false
            options:
                unsigned: false
            id: true
            generator:
                strategy: IDENTITY
    fields:
        nombre:
            type: string
            nullable: true
            length: 100
            options:
                fixed: false
        createdat:
            type: datetime
            nullable: true
            column: createdAt
        updatedat:
            type: datetime
            nullable: true
            column: updatedAt
    manyToOne:
        categoria:
            targetEntity: Categoria
            cascade: {  }
            fetch: LAZY
            mappedBy: null
            inversedBy: null
            joinColumns:
                categoria:
                    referencedColumnName: id
            orphanRemoval: false
    lifecycleCallbacks: {  }

categoria.orm.yml

BackendBundleEntityCategoria:
    type: entity
    table: categoria
    id:
        id:
            type: integer
            nullable: false
            options:
                unsigned: false
            id: true
            generator:
                strategy: IDENTITY
    fields:
        nombre:
            type: string
            nullable: true
            length: 50
            options:
                fixed: false
    lifecycleCallbacks: {  }

producto.php

<?php

namespace BackendBundleEntity;
use SymfonyComponentSecurityCoreUserUserInterface;

/**
 * Producto
 */
class Producto
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var string
     */
    private $nombre;

   /**
     * @var integer
     */
    private $categoria;

    /**
     * @var DateTime
     */
    private $createdAt;

    /**
     * @var DateTime
     */
    private $updatedAt;


    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set nombre
     *
     * @param string $nombre
     *
     * @return Producto
     */
    public function setNombre($nombre)
    {
        $this->nombre = $nombre;

        return $this;
    }

    /**
     * Get nombre
     *
     * @return string
     */
    public function getNombre()
    {
        return $this->nombre;
    }

    /**
     * Set categoria
     *
     * @param integer $categoria
     *
     * @return Producto
     */
    public function setCategoria($categoria)
    {
        $this->categoria = $categoria;

        return $this;
    }

    /**
     * Get categoria
     *
     * @return integer
     */
    public function getCategoria()
    {
        return $this->categoria;
    }

    /**
     * 
     */
    public function __toString(){

        return $this->id;
    }

    /**
     * Set createdAt
     *
     * @param DateTime $createdAt
     *
     * @return Producto
     */
    public function setCreatedAt($createdAt)
    {
        $this->createdAt = $createdAt;

        return $this;
    }

    /**
     * Get createdAt
     *
     * @return DateTime
     */
    public function getCreatedAt()
    {
        return $this->createdAt;
    }

    /**
     * Set updatedAt
     *
     * @param DateTime $updatedAt
     *
     * @return Producto
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }

    /**
     * Get updatedAt
     *
     * @return DateTime
     */
    public function getUpdatedAt()
    {
        return $this->updatedAt;
    }   

    /**
     * @var DateTime
     */
    private $createdat;

    /**
     * @var DateTime
     */
    private $updatedat;

}

推荐答案

你正试图通过 Twig 回显"一个对象.在这种情况下,类别"对象.您可能不想回显对象,而是想回显名称,例如;

You are trying to "echo" an object through Twig. In this case the "Categoria" object. Instead of echoing the object, you problably want to echo the name like;

{{ producto.categoria.nombre }} 

解决此问题的另一种方法是向 Categoria 实体添加一个 __toString 方法,例如 ;

Another way to solve this problem is adding a __toString method to the Categoria entity like ;

class Categoria
{
    ...

    /**
     * @var string
     */
    private $nombre;

    ...

    public function __toString()
    {
        return $this->nombre; 
    }
}

你在这里所做的就是告诉 PHP,无论何时你以字符串的形式访问对象Categoria",你都应该使用它的名字.因此,当您尝试 echo $categoria{{ categoria }} 时,PHP 将查找 __toString 方法并回显您在该方法中返回的任何内容.

What you do here is tell PHP that whenever you access the object "Categoria" as a string, you should use its name. So when you try echo $categoria or {{ categoria }} PHP will look for the __toString method and echo whatever you return in that method.

尽管我仍然更喜欢你明确地使用 {{ categoria.name }}

Allthough I still prefer you explicitly use {{ categoria.name }} !

 
精彩推荐
图片推荐