【发布时间】:2015-04-29 11:41:40
【问题描述】:
我有多个由 ManyToMany 和 ManyToOne 关联关联的实体,我无法在关联中显示不止一层的数据
例如。有3个实体连接。客户->地址->国家。在 TWIG 中我可以显示:
{{ customer.name }} // outputs name
{{ customer.address.postcode }} // outputs post code from Address entity
但是这个:
{{ customer.address.country.isocode2 }} //should output ISO code from country entity
输出 500 内部服务器错误:
Method "isocode2" for object "Doctrine\ORM\PersistentCollection" does not exist in AppBundle:tables:customers.html.twig at line 43
更多信息 客户实体,地址映射
/**
* @var \AppBundle\Entity\Address
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Address")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="defaultaddress_id", referencedColumnName="id")
* })
*/
private $address;
地址实体中的国家/地区映射
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Country", inversedBy="address")
* @ORM\JoinTable(name="address_country",
* joinColumns={
* @ORM\JoinColumn(name="address_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="country_id", referencedColumnName="id")
* }
* )
*/
private $country;
国家实体:
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=45, nullable=true)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="isocode2", type="string", length=45, nullable=true)
*/
private $isocode2;
/**
* @var string
*
* @ORM\Column(name="isocode3", type="string", length=45, nullable=true)
*/
private $isocode3;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Address", mappedBy="country")
*/
private $address;
/**
* Constructor
*/
public function __construct()
{
$this->address = new \Doctrine\Common\Collections\ArrayCollection();
}
如果我尝试这样做
{{ customer.address.country }}
我得到了
ContextErrorException: Catchable Fatal Error: Object of class Doctrine\ORM\PersistentCollection could not be converted to string in app/cache/dev/twig/1d/7c/3eec624c629866dcd530ea084487b111c573dbcba579efa7a6b315c46c7a.php line 120
【问题讨论】:
-
为实体提供映射
-
您可以在生成树枝时通过控制器传输 isocode,即使这是一种不好的做法。你能加入“客户”的[转储](twig.sensiolabs.org/doc/functions/dump.html)吗?
-
错误信息表明 country 是一个看起来很奇怪的数组。检查地址和国家/地区之间的映射或发布您的地址映射。
-
我添加了更多信息