0

我尝试在一个查询中加入多个表。

我的实体关系:

Entities\Person:
  type: entity
  table: person
  manyToOne:
    address:
      targetEntity: Address
      joinColumn:
        name: idaddress
        referencedColumnName: idaddress
  manyToMany:
    tags:
      targetEntity: Tag
      joinTable:
        name: persons_tags
        joinColumns:
          person_idperson:
            referencedColumnName: idperson
        inverseJoinColumns:
          tag_idtag:
            referencedColumnName: idtag

标签

Entities\Tag:
  type: entity
  table: tag
  manyToMany:
    sms:
      targetEntity: Sms
      mappedBy: tag

短信

  manyToMany:
    tag:
      targetEntity: Tag
      joinTable:
        name: sms_tags
        joinColumns:
          sms_idsms:
            referencedColumnName: idsms
        inverseJoinColumns:
          tag_idtag:
            referencedColumnName: idtag

这是一个有效的工作 SQL 语句:

SELECT DISTINCT person.lastname, person.firstname, address.mobileprivate, address.mobilebusiness FROM
    `address`,
    `person`,
    `persons_tags`,
    `sms_tags`
WHERE
    person.idaddress = address.idaddress
    AND person.idperson = persons_tags.person_idperson
    AND persons_tags.tag_idtag = sms_tags.tag_idtag
    AND sms_tags.sms_idsms = (myINPUT)

现在我尝试在 DQL 中加入所需的实体以获得相同的结果:

SELECT DISTINCT s, t, p, a 
FROM Entities\Sms s 
JOIN s.sms_idsms t 
JOIN t.person_idperson p 
JOIN p.idaddress a 
WHERE s.idsms = (myINPUT)

但我只得到一个错误:

PHP Fatal error:  Uncaught exception 'Doctrine\\ORM\\Query\\QueryException' with message '
SELECT DISTINCT s, t, p, a 
FROM Entities\\Sms s 
JOIN s.sms_idsms t 
JOIN t.person_idperson p 
JOIN p.idaddress a 
WHERE s.idsms = 2' in 
/var/www/ci_doc_rem/Codeigniter_Doctrine/source/application/libraries/Doctrine/ORM/Query/QueryException.php:39
Stack trace:
#0 /var/www/ci_doc_rem/Codeigniter_Doctrine/source/application/libraries/Doctrine/ORM/Query/Parser.php(429): Doctrine\\ORM\\Query\\QueryException::dqlError('SELECT DISTINCT...')
#1 /var/www/ci_doc_rem/Codeigniter_Doctrine/source/application/libraries/Doctrine/ORM/Query/Parser.php(925): Doctrine\\ORM\\Query\\Parser->semanticalError('Class Entities\\...')
#2 /var/www/ci_doc_rem/Codeigniter_Doctrine/source/application/libraries/Doctrine/ORM/Query/Parser.php(1561): Doctrine\\ORM\\Query\\Parser->JoinAssociationPathExpression()
#3 /var/www/ci_doc_rem/Codeigniter_Doctrine/source/application/libraries/Doctrine/ORM/Query/Parser.php(1506): Doctrine\\ORM\\Query\\Parser->JoinAssociationDeclaration()
#4 /var/www/ci_ in /var/www/ci_doc_rem/Codeigniter_Doctrine/source/application/libraries/Doctrine/ORM/Query/QueryException.php on line 49

如何编写像工作 SQL 查询一样的 DQL 查询?

我尽量避免使用原生 SQL 查询

这是查询生成器的尝试:

    $qb->select(array('s', 't', 'p', 'a'))
       ->from('Entities\Sms', 's')
       ->leftJoin('s.tag', 't')
       ->leftJoin('t.person', 'p')
       ->leftJoin('p.address', 'a')
       ->where('s.idsms = 1');

和错误信息

PHP Fatal error:  Uncaught exception 'Doctrine\\ORM\\Query\\QueryException' with message 'SELECT s, t, p, a FROM Entities\\Sms s LEFT JOIN s.tag t LEFT JOIN t.person p LEFT JOIN p.address a WHERE s.idsms = 1' in /var/www/ci_doc_rem/Codeigniter_Doctrine/source/application/libraries/Doctrine/ORM/Query/QueryException.php:39\nStack trace:\n#0 /var/www/ci_doc_rem/Codeigniter_Doctrine/source/application/libraries/Doctrine/ORM/Query/Parser.php(429): Doctrine\\ORM\\Query\\QueryException::dqlError('SELECT s, t, p,...')\n#1 /var/www/ci_doc_rem/Codeigniter_Doctrine/source/application/libraries/Doctrine/ORM/Query/Parser.php(925): Doctrine\\ORM\\Query\\Parser->semanticalError('Class Entities\\...')\n#2 /var/www/ci_doc_rem/Codeigniter_Doctrine/source/application/libraries/Doctrine/ORM/Query/Parser.php(1561): Doctrine\\ORM\\Query\\Parser->JoinAssociationPathExpression()\n#3 /var/www/ci_doc_rem/Codeigniter_Doctrine/source/application/libraries/Doctrine/ORM/Query/Parser.php(1506): Doctrine\\ORM\\Query\\Parser->JoinAssociationDeclaration()\n#4 /var/www/ci_doc_rem/Cod in /var/www/ci_doc_rem/Codeigniter_Doctrine/source/application/libraries/Doctrine/ORM/Query/QueryException.php on line 49
4

1 回答 1

0

解决方案很容易找到。

我已将每个表/实体的 idname 命名约定替换为“id”,并仔细研究了 Picking Owning 和 Inverse Side。

http://docs.doctrine-project.org/en/latest/reference/association-mapping.html#picking-owning-and-inverse-side

这使我能够编写正确的关系。

对我来说,一个大问题是解码调试堆栈跟踪以找到我的错误。

感谢任何花时间解决我的问题的人!

于 2013-05-03T14:31:03.547 回答