【问题标题】:Symfony2, how to display data from entities with ManyToMany associationsSymfony2,如何显示来自具有多对多关联的实体的数据
【发布时间】: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 是一个看起来很奇怪的数组。检查地址和国家/地区之间的映射或发布您的地址映射。
  • 我添加了更多信息

标签: php symfony doctrine twig


【解决方案1】:

通过多对多关联,一个地址可以有多个国家。这对您的逻辑是否正确?

如果是,您必须遍历地址的所有国家/地区:

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

如果您的地址只有一个国家/地区,则必须使用多对一关联。然后你可以使用你的语法:

{{ customer.address.country.isocode2 }}

【讨论】:

  • 您说得对,先生。我已经设法正确设置它,但由于某种原因 php app/console dictionary:schema:update 试图在 customer.address 上的数据库中创建唯一键
  • 有时是 MySQL 索引的问题。如果您正在开发中,请尝试删除数据库并重新创建它。
  • 我想通了。由于实体中的代码而生成了密钥。我已经从数据库方案生成了实体,并将密钥移到了实体中,所以当我尝试从实体更新 db 时,它也在生成该密钥。愚蠢的错误。
猜你喜欢
  • 2023-01-12
  • 1970-01-01
  • 2016-05-11
  • 2023-03-16
  • 2023-03-05
  • 2013-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多