【问题标题】:Object of class \BackendBundle\Entity\Categoria could not be converted to string类 \BackendBundle\Entity\Categoria 的对象无法转换为字符串
【发布时间】:2017-10-01 01:00:15
【问题描述】:

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

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 打印产品时,它会显示除类别编号之外的所有字段,我收到错误消息:

在渲染模板期间引发了异常(“Catchable >Fatal Error: Object of class Proxies__CG__\BackendBundle\Entity\Categoria >无法转换为字符串”)。

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


producto.orm.yml

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;

}

【问题讨论】:

  • 在某些时候,您尝试类似echo $something,其中$somethingCategoria 的一个实例。如果要打印所有属性,请为此目的使用适当的函数,例如print_rvar_dump.
  • 这是关于渲染错误...你能告诉我们你使用的树枝代码吗?
  • {{ producto.categoria.nombre }}
  • 感谢 Tobias F. 和 Veve!解决方案是 Arne 说的 {{ producto.categoria.nombre }} 非常感谢!
  • 好吧!!

标签: php symfony twig


【解决方案1】:

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

{{ producto.categoria.nombre }} 

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

class Categoria
{
    ...

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

    ...

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 2015-07-27
    • 2012-04-29
    相关资源
    最近更新 更多