Php Symfony2,如何显示来自具有多个关联的实体的数据

Php Symfony2,如何显示来自具有多个关联的实体的数据,php,symfony,doctrine,twig,Php,Symfony,Doctrine,Twig,我有多个实体由多个多个和多个一个关联关联关联,我在显示多个层次的关联数据时遇到问题 比如说。有3个实体连接在一起。客户->地址->国家。在TWIG中,我可以显示: {{ customer.name }} // outputs name {{ customer.address.postcode }} // outputs post code from Address entity 但这是: {{ customer.address.country.isocode2 }} //should outp

我有多个实体由多个多个和多个一个关联关联关联,我在显示多个层次的关联数据时遇到问题

比如说。有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

对于多个关联,一个地址可以有多个国家/地区。这符合你的逻辑吗

如果是,您必须重复访问地址的所有国家:

{% for country in customer.address.country %}
    {{ country.isocode2 }}
{% endfor %}
如果您的地址只有一个国家/地区,则必须使用多个同一协会。然后您可以使用您的语法:

{{ customer.address.country.isocode2 }}

为实体提供映射。生成细枝时,您可以通过控制器传输等位代码,即使这是一种不好的做法。你能加入“customer”的[dump]()吗?错误消息表明country是一个看起来很奇怪的数组。检查您的地址和国家之间的映射,或者发布您的地址映射。我添加了更多信息。您是对的,先生。我已经成功地将其正确设置,但出于某种原因,php应用程序/控制台条令:schema:update正试图在customer.address上的数据库中创建唯一的密钥。有时,这是MySQL索引的问题。如果您正在开发中,请尝试删除数据库并重新创建它。我知道这一点。由于实体中的代码而生成密钥。我已经从数据库方案生成了实体,它将密钥移动到了实体,所以当我试图从实体更新数据库时,它也生成了那个密钥。愚蠢的错误。