Doctrine - [语义错误] - 错误:类没有字段或关联

时间:2015-01-11 19:09:11

标签: php doctrine-orm

我正在尝试使用Doctrine的DQL在存储库中运行查询,我收到以下错误:

  

QueryException.php第63行中的QueryException:   [语义错误]第0行,第100列附近' test_suite_id':错误:类Application \ Model \ Entity \ Page没有名为test_suite_id的字段或关联

协会

User可以有多个TestSuiteTestSuite可以有多个Page。因此,我在UserTestSuite以及TestSuitePage之间分别有一对多关联。

Associations

以下是我的实体,只显示了相关的关联:

用户

/**
 * @ORM\Table(name="users", uniqueConstraints={@ORM\UniqueConstraint(name="unique_email", columns={"email"})})
 * @ORM\Entity(repositoryClass="Application\Model\Repository\UserRepository")
 */
class User
{
    /**
     * @var TestSuite[]
     *
     * @ORM\OneToMany(targetEntity="TestSuite", mappedBy="user", cascade={"all"})
     */
    private $testSuites = [];
}

的TestSuite

/**
 * @ORM\Table(name="test_suites")
 * @ORM\Entity(repositoryClass="Application\Model\Repository\TestSuiteRepository")
 */
class TestSuite
{
    /**
     * @var User
     *
     * @ORM\ManyToOne(targetEntity="User", inversedBy="testSuites")
     * @ORM\JoinColumn{name="user_id", referencedColumnName="id")
     */
    private $user;

    /**
     * @var Page[]
     *
     * @ORM\OneToMany(targetEntity="Page", mappedBy="testSuite", cascade={"all"})
     */
    private $pages = [];

}

/**
 * @ORM\Table(name="pages")
 * @ORM\Entity(repositoryClass="Application\Model\Repository\PageRepository")
 */
class Page
{
    /**
     * @var TestSuite
     *
     * @ORM\ManyToOne(targetEntity="TestSuite", inversedBy="pages")
     * @ORM\JoinColumn(name="test_suite_id", referencedColumnName="id", nullable=false)
     */
    private $testSuite;
}

存储库DQL查询

public function findPageForUser(User $user, $pageId)
{
    $qb = $this->getEntityManager()->createQueryBuilder();

    $qb->add('select', 'p')
       ->from('Application\Model\Entity\Page', 'p')
       ->join('Application\Model\Entity\TestSuite', 'ts', 'WITH', 'p.test_suite_id = ts.id')
       ->join('Application\Model\Entity\User', 'u', 'WITH', 'ts.user_id = u.id')
       ->where('p.id = :pageId')
       ->andWhere('u = :user')
       ->setParameter('user', $user)
       ->setParameter('pageId', $pageId);

    return $qb->getQuery()->getResult();
}

请注意,我只是传递User$pageId。由查询本身决定用户通过测试套件加入是否具有该特定页面。

正如您所看到的,Page实体显然有一个test_suite_id外键列指向id实体的TestSuite列,我的架构反映了这一点。

生成的查询

  

SELECT p FROM Application \ Model \ Entity \ Page p INNER JOIN Application \ Model \ Entity \ TestSuite ts WITH p.test_suite_id = ts.id INNER JOIN Application \ Model \ Entity \ User u WITH ts.user_id = u.id WHERE p.id =:pageId AND u =:user

其他帖子建议如下:

  • 清除缓存 - 我没有启用缓存层
  • 重启Apache - 没有区别
  • 重命名" Colum" to" Column" - 我不是那个傻瓜
  • 将FQDN放入targetEntity - 他们位于同一目录中,因此无需

为什么我会特别提到这个问题,我该怎么办?

1 个答案:

答案 0 :(得分:10)

DQL是静态类型的,DQL语法是关于对象属性,而不是SQL列。

此外,无需直接选择关联实体:连接由DQL查询组件自动放置,并在结果SQL中正确生成所需的ON子句。

以下是您应该运行的查询版本:

SELECT
    p
FROM
    Application\Model\Entity\Page p
INNER JOIN
    p.testSuite AS ts
INNER JOIN
    ts.user u
WHERE
    p.id = :pageId
    AND u = :user

由于Application\Model\Entity\TestSuite#$userto-one关联,您可以通过删除一个连接条件来进一步简化查询:

SELECT
    p
FROM
    Application\Model\Entity\Page p
INNER JOIN
    p.testSuite AS ts
WHERE
    p.id = :pageId
    AND ts.user = :user

以上查询的后续关联DQL构建器代码如下:

public function findPageForUser(User $user, $pageId)
{
    $qb = $this->getEntityManager()->createQueryBuilder();

    $qb->add('select', 'p')
       ->from('Application\Model\Entity\Page', 'p')
       ->join('Application\Model\Entity\TestSuite', 'ts')
       ->where('p.id = :pageId')
       ->andWhere('ts.user = :user')
       ->setParameter('user', $user)
       ->setParameter('pageId', $pageId);

    return $qb->getQuery()
              ->setFetchMode("Application\Model\Entity\Page", "elements", "EAGER")
              ->getResult();
}