本文介绍了类 \BackendBundle\Entity\Categoria 的对象无法转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;

当用树枝打印产品时,它显示了所有字段,除了类别号之一,我收到错误:

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__\BackendBundle\Entity\Categoria >无法转换为字符串").

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

Any idea to solved this issue?

BackendBundle\Entity\Producto:
    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

BackendBundle\Entity\Categoria:
    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 BackendBundle\Entity;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 * 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 }} !

这篇关于类 \BackendBundle\Entity\Categoria 的对象无法转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 01:54