簡體   English   中英

如何在學說中搜索多個標簽

[英]how to search multiple tags in doctrine

我有一個數據庫,其中包含有關標簽和人員的多對多關系。 嘗試搜索具有多個標簽的人時遇到麻煩。 我嘗試了這個,但是失敗了:

        $person = Doctrine_Query::create()
        ->from("Model_Person p")
        ->innerJoin("p.tags t")
        ->whereIn('t.id',$t)
        ->execute();

上面的語句返回在數組$ t中至少具有標簽之一的所有人員,但是我只希望在該數組中具有“所有”標簽的人員。

有人知道如何實現嗎?

謝謝

這是因為IN返回至少匹配一個的所有結果。 假設您有id IN(1,2,3)它等於id = 1 OR id = 2 OR id = 3

要歸檔您期望的目標,請使用Doctrine的andWhere()方法,將每個值現在都包含在$t例如[...]->andWhere('id = ?', 1)->andWhere('id = ?', 2)->[...]

經過一段時間的研究,我發現並采用了完全符合我需要的純SQL代碼。 在我的應用程序中,rizidoro的人員表是信息表。

   //$tagQueryString contains tags separated by space ex. "crisis usa"    
   $tagsArray = explode(' ', $tagQueryString);
   $tagsArrayQuery = '"' . implode('","', $tagsArray) . '"';
   //$tagsArrayQuery='"crisis","usa";


    $fromQuery =
            '
        info i
   INNER JOIN (SELECT   it.info_id
               FROM     info_tag it
                        INNER JOIN info i
                          ON i.id = it.info_id
                        INNER JOIN tag t
                          ON t.id = it.tag_id
               WHERE    t.name IN (' . $tagsArrayQuery . ')
               GROUP BY it.info_id
               HAVING   COUNT(it.info_id) = ' . count($tagsArray) . ') ii
     ON i.id = ii.info_id';

    $query = new Doctrine_RawSql();
    $query->select('{i.*}');
    $query->from($fromQuery);
    $query->addComponent('i', 'Model_Info i');

  //you can add offset and limit for pagination
    $infos = $query->limit($resultsPerPage)
                   ->offset($resultsPerPage * ($currentPage - 1))
                   ->execute();

那不是優雅的代碼,但是可以正常工作。 我不知道如何做得更像“教義式”。 以下是針對類似問題的其他解決方案: http : //www.sergiy.ca/how-to-write-many-to-many-search-queries-in-mysql-and-hibernate

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM