2016-08-18 86 views
8

我正面臨一個亟待解決的問題。我已經編寫了分頁代碼。一切都按預期工作,但只有條件不起作用(只有某些條件) 這是我的分頁代碼。Cake Php paginator問題

//declaration 
public $paginate = array(
     'limit' => 50, 
     'paramType' => 'querystring' 
    ); 
//use in action 
$this->paginate['ApiLog'] = array('limit' => 50, 'order' => 'ApiLog.id DESC', 'paramType' => 'querystring'); 

$this->paginate['ApiLog']['conditions'] = array('ApiLog.log_type' => 2); 
$this->paginate['ApiLog']['joins'] = array(    
      array(
       'table' => 'users', 
       'alias' => 'User', 
       'type' => 'LEFT', 
       'conditions' => array('User.id = ApiLog.user_id') 
      )     
     ); 
$this->Paginator->settings = $this->paginate['ApiLog']; 
$apilogs = $this->Paginator->paginate('ApiLog'); 

這個代碼在開發環境知府並返回患有2型日誌,但它在生產只返回的類型爲1.我花了整整一天,以找出問題的日誌。 如果我確實在conditions array中添加了其他任何條件,但是不會生效log_type。 我打印查詢日誌,在where clause它總是顯示log_type = '1' 我也清除了緩存。 任何幫助表示感謝,謝謝。

+0

試着刪除你的'tmp/cache'文件夾 – om1

回答

2

我有固定的問題,這個問題是由於data type宣佈進入數據庫。在生產中,它是tinyint(1),在開發中它給出了int(1)。它正在兩個環境中保存數據庫的正確值,但在條件tinyint(1)正在使用只有0 & 1不與2

我不明白在這背後的原因。

2

如果你的代碼在本地機器上工作正常,但不能在生產環境中工作,那麼你必須清除tmp/cache/modeltmp/cahe/persistance中的所有文件。請確保您已給予tmp文件夾的可寫入權限。

這意味着當您添加新字段,刪除現有字段或對數據庫模式進行任何更改時,您必須更新Cake Model模式。在生產模式下如果未找到模式緩存文件,則會根據數據庫上的當前模式創建新的緩存文件。在每次執行的開發模式中,它都會更新模式緩存。

仍然在分頁問題,​​然後你必須遵循CakePHP 2.x文檔。這裏我假設你的代碼是ApiLogsController,方法是index。根據您的代碼應該是文檔。

public function index() { 
    $this->Paginator->settings = array(
     'limit' => 50, 
     'order' => 'ApiLog.id DESC' 
     'paramType' => 'querystring', 
     'conditions'=> array('ApiLog.log_type' => 2), 
     'recursive'=>-1, // should be used with joins 
     'joins'=>array(
      array(
       'table'=>'users', 
       'type'=>'LEFT', 
       'alias'=>'User', 
       'conditions'=>array('User.id = ApiLog.user_id') 
      ) 
     ) 
    ); 
    $data = $this->Paginator->paginate('ApiLog'); 
    $this->set(compact('data')); 
} 

OR

// your default setting in ApiLogController class 
public $components = array('Paginator'); 
public $paginate = array(
    'limit' => 50, 
    'paramType' => 'querystring' 
); 

public function index() { 
// overriding/extending default settings 

$this->Paginator->settings['order']='ApiLog.id DESC'; 
$this->Paginator->settings['conditions']=array('ApiLog.log_type' => 2), 
$this->Paginator->settings['recursive']= -1; 
$this->Paginator->settings['joins']=array(
     array(
      'table'=>'users', 
      'type'=>'LEFT', 
      'alias'=>'User', 
      'conditions'=>array('User.id = ApiLog.user_id') 
     ) 
    ); 

$data = $this->Paginator->paginate('ApiLog'); 
$this->set(compact('data')); 
} 
+0

我已經清除了緩存。另外我的Paginator設置是正確的。我在設置中添加任何其他條件,然後它的工作 – Karan

+0

@Karan,所以現在你的問題已經解決? –

+0

nope,問題仍然存在 – Karan