简体   繁体   English

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

[英]Symfony2, how to display data from entities with ManyToMany associations

I have multiple entities associated by ManyToMany and ManyToOne associations and I have problem with displaying data more than one level deep in to associations 我有由ManyToMany和ManyToOne关联关联的多个实体,并且在显示比关联深一层的数据时遇到问题

For example. 例如。 There are 3 entities connected. 有3个实体连接。 Customer->Address->Country. 客户 - >地址 - >国家。 In TWIG I can display: 在TWIG中,我可以显示:

{{ customer.name }} // outputs name
{{ customer.address.postcode }} // outputs post code from Address entity

But this: 但是这个:

{{ customer.address.country.isocode2 }} //should output ISO code from country entity

Outputs 500 internal server error: 输出500内部服务器错误:

Method "isocode2" for object "Doctrine\ORM\PersistentCollection" does not exist in AppBundle:tables:customers.html.twig at line 43

More info Customer entity, address mapping 更多信息客户实体,地址映射

/**
 * @var \AppBundle\Entity\Address
 *
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Address")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="defaultaddress_id", referencedColumnName="id")
 * })
 */
private $address;

Country mapping in address entity 地址实体中的国家/地区映射

/**
 * @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;

Country entity: 国家实体:

    /**
 * @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();
}

If I try to do this 如果我尝试这样做

{{ customer.address.country }}

I am getting 我正进入(状态

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

With ManyToMany association, an Address can have multiple countries. 通过ManyToMany关联,一个地址可以有多个国家/地区。 Is this correct for your logic? 这是否符合您的逻辑?

If it is, you have to iterate on all countries of Address: 如果是这样,则必须遍历地址的所有国家/地区:

{% for country in customer.address.country %}
    {{ country.isocode2 }}
{% endfor %}

If your address have only one country, you must use ManyToOne association. 如果您的地址只有一个国家/地区,则必须使用ManyToOne关联。 Then you can use your syntax: 然后,您可以使用语法:

{{ customer.address.country.isocode2 }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM