Doctrine - [Semantic error] - Error: the class does not have a field or association

I try to run a query with Doctrine DQL in the repository and I get the following error:

QueryException in line QueryException.php 63: [Semantic error] line 0, col 100 next to 'test_suite_id': Error: class application \ model \ entity \ page does not have a field or association with the name test_suite_id

Associations

A Usermay have several TestSuites. A TestSuitemay have several Pages. As a result, I have a One-to-many relationship between Userand TestSuiteand TestSuiteand, Pagerespectively.

Associations

Here are my objects with only the relevant associations shown:

User

/**
 * @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 = [];

}

Page

/**
 * @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 Query Repository

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 \\\ u ts.user_id = u.id WHERE p.id =: pageId AND u =: user

:

  • - .
  • Apache -
  • "Colum" "Column" - .
  • targetEntity - ,

, , ?

+4
1

DQL , DQL - , SQL.

, : DQL ON SQL.

, :

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#$user to-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();
}
+9

Source: https://habr.com/ru/post/1570787/


All Articles