【问题标题】:Entity relationship is not recognied by the join column连接列无法识别实体关系
【发布时间】:2014-08-29 10:19:08
【问题描述】:

我试图在Monthly 表中查找与Category 关联的所有结果。 我做了我的研究,下面的代码理论上应该可以工作,但不知何故抛出了以下 错误 Notice: Undefined index: joinColumns in...

表通过连接表连接,使用的键是 category_idmonthly_id 尝试使用 category_id 运行查询,但仍然不起作用并引发另一个错误。

当我运行orm:validate-schema 时,它似乎一切正常。我错过了什么?

来自每月实体

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="Bnk\Entity\Category", inversedBy="monthly")
     * @ORM\JoinTable(name="months_categories",
     *   joinColumns={
     *     @ORM\JoinColumn(name="monthly_id", referencedColumnName="id")
     *   },
     *   inverseJoinColumns={
     *     @ORM\JoinColumn(name="category_id", referencedColumnName="id")
     *   }
     * )
     */
    private $category;

为了找到与给定Category 关联的所有Monthly,我缺少什么?

$months = $this->getEntityManager()
    ->getRepository('Bnk\Entity\Monthly')
    ->findBy(
        array(
            "category"=>$category->getId()
        )
);

【问题讨论】:

  • 你确定它在这个特定的关系上失败了吗?尝试删除此关系配置,看看错误是否消失。
  • @NDM 如果我确实 findAll 它工作正常
  • findBy 无法处理多对多关系。 stackoverflow.com/questions/13343533/…
  • 并考虑将 Monthly#category 重命名为类别。清楚地表明您在处理一系列类别。

标签: php orm doctrine-orm zend-framework2 doctrine


【解决方案1】:

您是否忘记了初始化 ArrayCollection? 在您的实体构造函数中,您始终必须初始化它们。

所以在你的班级中名列前茅:

use Doctrine\Common\Collections\ArrayCollection;

和你的构造函数:

public function __construct()
{
    $this->category= new ArrayCollection();
}

【讨论】:

  • 教义实体创建者自动为我做这件事
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-16
  • 2016-07-01
  • 2019-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-24
相关资源
最近更新 更多