Doctrine 多对多查询生成器

Doctrine many to many query builder

我有一个 Contacts 实体和一个具有 多对多 关系的 Users 实体,以便设置常用联系人。

我尝试构建一个查询来查找 联系人 是否是 用户 的最爱。 Doctrine returns 出现以下错误:

[Semantical Error] line 0, col 81 near 'favoris = :user': Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected.

这是我的查询:

    public function isFavori(users $user, contacts $contacts) {

    $em = $this->getEntityManager();
    $qb = $em->createQueryBuilder('c');
    $query = $qb
        ->select('1')
        ->from('contactsBundle:contacts', 'c')
        ->join('contactsBundle:users', 'f', 'WITH', 'c.favoris = :user')
        ->where('c = :contact')
        ->setParameter('user', $user)
        ->setParameter('contact', $contacts)
    ;
    $querystring = $qb->getQuery();
    return $qb->getQuery()->getResult();

}

这是来自 Contact 实体的连接注释:

/**
 * @ORM\ManyToMany(targetEntity="Curuba\contactsBundle\Entity\users", inversedBy="contactsfavoris")
 *
 */
private $favoris;

首先,您需要向多对多关联添加一个连接表:

/**
 * @ORM\ManyToMany(targetEntity="Curuba\contactsBundle\Entity\users", inversedBy="contactsfavoris")
 * @JoinTable(name="user_contact_favoris")
 */
private $favoris;

现在您的查询:

public function isFavori(users $user, contacts $contact) {

    $em = $this->getEntityManager();
    $qb = $em->createQueryBuilder('c');
    $qb->select('1')
        ->from('user_contact_favoris', 'f')
        ->innerJoin('contactsBundle:contacts', 'c', 'WITH', 'f.contact = :contact')
        ->innerJoin('contactsBundle:users', 'u', 'WITH', 'f.user = :user')
        ->setParameter('user', $user)
        ->setParameter('contact', $contact)
    ;

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

希望对您有所帮助

我最终创建了自己的 class。这也需要在使用此包时获得更大的灵活性来操作查询:APYDataGridBundle.

这就是我所拥有的:

contacts 实体 class,具有:

/**
 * @ORM\OneToMany(targetEntity="Curuba\contactsBundle\Entity\contactsfavoris", mappedBy="contact", orphanRemoval=true, cascade={"remove", "persist"})
 * @ORM\JoinColumn(nullable=true)
 */
private $usersFavoris;

users 实体 class,具有:

/**
 * @ORM\OneToMany(targetEntity="Curuba\contactsBundle\Entity\contactsfavoris", mappedBy="user")
 *
 */
private $contactsfavoris;

contactsfavoris 实体 class,具有:

/**
 * @ORM\ManyToOne(targetEntity="contacts", inversedBy="usersFavoris")
 */
private $contact;

/**
 * @ORM\ManyToOne(targetEntity="users", inversedBy="contactsfavoris")
 */
private $user;

查询:

public function isFavori(users $user, contacts $contacts) {

    $em = $this->getEntityManager();
    $qb = $em->createQueryBuilder('c');
    $userid = $user->getId();
    $contactId = $contacts->getId();
    $query = $qb
        ->select('1')
        ->from('contactsBundle:contactsfavoris','cf')
        ->where('cf.user = :user')
        ->andWhere('cf.contact = :contact')
        ->setParameter('user', $userid)
        ->setParameter('contact', $contacts)
    ;

    try {
        $result = $qb->getQuery()->getSingleScalarResult();
    } catch (Exception $e) {
        $result = 0;
    }
    if ($result == 1 ) {
        $favori = true;
    } else {
        $favori = false;
    }

    return $favori;

}