2015-01-31 72 views
0

我有一個聯繫人實體和一個用戶實體與多對多的關係來設置收藏夾。學說多對多的查詢生成器

我嘗試構建查詢來查找聯繫人是否是用戶的最愛。學說返回以下錯誤:

[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(); 

} 

下面是從接觸實體加入註釋:

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

回答

0

我創造我自己的類結束了。在使用該軟件包時,這也是爲了更靈活地操作查詢所必需的:APYDataGridBundle

因此,這裏是我:

contacts.php:

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

users.php:

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

contactsfavoris.php

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

} 
1

首先,你需要一個JoinTable增加ManyToManyAssociation:

/** 
* @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(); 
} 

希望它可以幫助

+0

謝謝你的幫助。我們爲什麼要指定JoinTable名稱?學說自動創建它?你是說查詢對象只能用於一對一或一對多的關係嗎? – curuba 2015-02-01 08:43:42

+0

可能是這種情況,因爲如果我使用'' - >和Where('f.id =:user')''那麼它就可以工作。唯一的區別是我使用一個id而不是對象本身... – curuba 2015-02-01 08:48:02

+0

當我嘗試你的建議時,它會觸發以下錯誤:'Class'Curuba \ contactsBundle \ Entity \ contacts_users'沒有被定義.''。當我用''contactsBundle:contacts_users''嘗試時,它也不起作用。 您是否需要實際創建類來描述自己的多對多關係? – curuba 2015-02-01 11:38:41