2011-05-11 127 views
0

我們使用zend的框架和工作原理數據庫。 現在我的問題是我想搜索一個字符串(例如:testin'g)與「單引號」字符串。 原則查詢無法爲連接的查詢檢索數據,並且可以正常查詢且無連接。學說---搜索

我的方法是相反addWhere的

public function getNewsListGrid($campid,$currentPage,$resultsPerPage,$quickSearchType,$queryv,$sortName,$sortOrder,$letterPressed) 
{ 
    $query = new Doctrine_Query(); 
    $whereCond =''; 
    $where='eng.id='.$campid; 
    $select=''; 
    $search = ''; 

    if($letterPressed == 'All') 
    { 
     $where .=""; 
    } 
    else if($letterPressed == 'Radian6') 
    { 
     $where .=" AND c.news_type = 1"; 
    } 
    else if($letterPressed == 'Google News') 
    { 
     $where .=" AND c.news_type = 3"; 
    } 
    else if($letterPressed == 'Google Blogs') 
    { 
     $where .=" AND c.news_type = 4"; 
    } 
    else if($letterPressed == 'Hide Twitter') 
    { 
     $where .=" AND c.url NOT LIKE '%twitter.com%'"; 
    } 
    else if($letterPressed == 'Hide Facebook') 
    { 
     $where .=" AND c.url NOT LIKE '%facebook.com%'"; 
    } 
    else if($letterPressed == 'Hide Facebook-twitter') 
    { 
     $where .=" AND c.url NOT LIKE '%twitter.com%' AND c.url NOT LIKE '%facebook.com%'"; 
    } 
    else if($letterPressed == 'Show Twitter') 
    { 
     $where .=" AND c.url LIKE '%twitter.com%'"; 
    } 
    else if($letterPressed == 'Show Facebook') 
    { 
     $where .=" AND c.url LIKE '%facebook.com%'"; 
    } 
    else if($letterPressed == 'show Facebook-twitter') 
    { 
     $where .=" AND c.url LIKE '%twitter.com%' OR c.url LIKE '%facebook.com%'"; 
    } 


    if($queryv!='' && (trim($letterPressed) != 'Law' && trim($letterPressed) != 'Ls Translate')){ 

     $search = ' c.title like "%'.mysql_real_escape_string($queryv).'%" '; 
    } 

    $select='c.id, c.campaign_id, c.title as title, c.domain, c.news_type,c.url, c.active, c.article_date, c.created_date, c.modified_date, t.language_id, t.tr_translated_date, t.tr_translated_by, t.translation_state, t.tr_title, t.raw_title'; 
    if(trim($letterPressed) == 'Original') 
    { 
     $select='c.id, c.campaign_id, c.title as title, c.domain, c.news_type,c.url, c.active, c.article_date, c.created_date, c.modified_date, t.language_id, t.tr_translated_date, t.tr_translated_by, t.translation_state'; 
    } 
    if(trim($letterPressed) == 'Law') 
    { 
     $select='c.id, c.campaign_id, c.domain, c.news_type,c.url, c.active,c.article_date, c.created_date, c.modified_date, t.raw_title as title, t.language_id, t.tr_translated_date, t.tr_translated_by, t.translation_state'; 
     if($queryv!='') 
      $search = ' t.raw_title like "%'.mysql_real_escape_string($queryv).'%" '; 
    } 
    if(trim($letterPressed) == 'Ls Translate') 
    { 
     $select='c.id, c.campaign_id, c.domain, c.news_type,c.url, c.active, c.article_date, c.created_date, c.modified_date, t.tr_title as title, t.language_id, t.tr_translated_date, t.tr_translated_by, t.translation_state'; 
     $where .=" AND t.translation_state = 1"; 
     if($queryv!='') 
      $search = ' t.tr_title like "%'.mysql_real_escape_string($queryv).'%" '; 
    } 
    $whereCond.=$where; 

    $query->select($select) 
      ->from('News c') 
      ->leftJoin('c.TranslatedNews t') 
      ->leftJoin('c.Campaigns eng') 
      ->where($whereCond) 
      ->addWhere('eng.tr_language_id=t.language_id') 
      ->orderBy('c.'.$sortName. ' ' . $sortOrder) 
      ->groupBy('c.title') 
      ->addGroupBy('c.url'); 
     if(!empty($search)) 
      $query->addWhere($search); 
     //echo $query->getSqlQuery();exit; 
     /* 
          here when i output query and run in any query browser it works fine. 
     */ 
      $pager = new Doctrine_Pager($query,$currentPage, $resultsPerPage);// here it fails to retrive data 

    return $pager;//print_r($pager);exit; 

回答

1

嘗試使用andWhere()

+0

我試過,但沒有工作 – user1155659 2011-05-11 09:28:57

0

我用這樣的

$q = Doctrine_Query::create() 
          ->select('t.*') 
          ->from('TransferReason t'); 
      if ($searchValue != "") { 
       $q->where($feildName.' LIKE ?', '%' . trim($searchValue) . '%'); 
      } 
      $q->orderBy($orderField . ' ' . $orderBy); 
+0

我要搜索的字符串,例如使用情況,而不是多個:testin'g對我來說,搜索罰款單表,但是當我加入多張桌子時,它並不是。 – user1155659 2011-05-11 09:36:07

0

這裏例如加入例如

$q = Doctrine_Query::create() 
            ->from('TransferRequest t') 
            ->leftJoin('t.Employee e ON t.emp_number = e.emp_number') 
            ->leftJoin('e.CompanyStructure c ON e.work_station = c.id') 
            ->where("c.title LIKE ?", "%{$cond}%"); 
+0

yaa對於我正常的搜索(ex字符串:測試)的作品,但當字符串包含單引號(ex字符串:testin'g),它不起作用 – user1155659 2011-05-11 09:42:36

+0

使用mysql_real_escape_string函數 $ query = sprintf(「SELECT * FROM users WHERE user ='%s'AND password ='%s'「, mysql_real_escape_string($ user), mysql_real_escape_string($ password)); – 2011-05-11 09:44:22

+0

我用mysql_real_escape_string也不過它dosent工作,我請看看我的查詢一次 – user1155659 2011-05-11 09:53:09

0

主義處理escapi內部。使用類似的查詢:

$q = Doctrine_Query::create()->from('MyTable m')->where('m.column = ?', '?´## \'´^#11 " dfvü:?=._ßß?(%%/'); 

和人物將被轉義成爲你的數據庫的安全。

0

月的問題是接合期間,因爲我總是寫這樣的:

$query->select($select) 
     ->from('News', 'c') 
     ->leftJoin('c.TranslatedNews', 't') 
     ->leftJoin('c.Campaigns', 'eng') 
     ->where($whereCond) 
     ->andWhere('eng.tr_language_id=t.language_id') 
     ->orderBy('c.'.$sortName. ' ' . $sortOrder) 
     ->groupBy('c.title, c.url'); 
    if(!empty($search)) 
     $query->addWhere($search); 

我找不到$ sortName和$中將sortOrder所以也許你錯過了一些變數。

我建議如果對$其中(更少的代碼行:))