【问题标题】:Doctrine ORM semantical query error: Class has no associationDoctrine ORM语义查询错误:类没有关联
【发布时间】:2017-06-29 13:17:35
【问题描述】:

得到一个有点抽象的错误,存储库和实体关系映射似乎是正确的:

[Semantical Error] line 0, col 102 near 'v WHERE t.domainName': Error: 
    Class AppBundle\Entity\DocumentVersion has no association named document_versions

Document 实体:

/**
 * @var Collection|DocumentVersion[]
 *
 * @ORM\OneToMany(targetEntity=DocumentVersion::class, mappedBy="document")
 **/
private $document_versions;

DocumentVersion 实体:

/**
 * @var Document
 *
 * @ORM\ManyToOne(targetEntity=\AppBundle\Entity\Document::class, inversedBy="document_versions")
 * @JoinColumn(name="document_id", referencedColumnName="id")
 **/
private $document;

一切似乎都被正确定义了。是什么导致了这个错误?

【问题讨论】:

  • 看到您已经回答了您的问题。作为你的一个附带问题:你为什么将你的实体属性设为private 而不是protected?我总是让我的protected 能够通过继承使用它们。想知道您是否出于特定原因使用 private
  • 我倾向于留下 private 直到我知道我想公开它们。

标签: symfony doctrine-orm repository-pattern


【解决方案1】:

同一错误的其他答案提供了不适用于我的情况的关系配置解决方案。相反,在检查app.repository.document_version 定义是否正确之后:

app.repository.document_version:
    class: AppBundle\Repository\DocumentVersionRepository
    factory: ['@doctrine', getRepository]
    arguments: ['AppBundle\Entity\DocumentVersion']

原来是DocumentVersion实体类中repository关系的配置:

/**
 * DocumentVersion
 *
 * @ORM\Table(name="document_version")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\DocumentRepository")
 * @ORM\HasLifecycleCallbacks
 * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
 */
class DocumentVersion

手术线:

 * @ORM\Entity(repositoryClass="AppBundle\Repository\DocumentRepository")

请注意,在上面的行中,它是如何指向错误的存储库的。一旦更新为:

 * @ORM\Entity(repositoryClass="AppBundle\Repository\DocumentVersionRepository")

这解决了问题。此外,在 Symfony(3.3) 中,它所使用的实际 DQL 是在第二个异常中向下滚动屏幕:

SELECT d 
FROM AppBundle\Entity\DocumentVersion d 
INNER JOIN d.tenant t 
INNER JOIN d.document_versions v 
WHERE t.domainName = :domain_name 
ORDER BY d.name ASC

希望这可以防止某人不必要地旋转车轮。 :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-05
    • 2020-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-07
    • 2014-03-12
    相关资源
    最近更新 更多