【发布时间】: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,其中$something是Categoria的一个实例。如果要打印所有属性,请为此目的使用适当的函数,例如print_r或var_dump. -
这是关于渲染错误...你能告诉我们你使用的树枝代码吗?
-
{{ producto.categoria.nombre }}
-
感谢 Tobias F. 和 Veve!解决方案是 Arne 说的 {{ producto.categoria.nombre }} 非常感谢!
-
好吧!!