Cakephp 3 - 从分页中删除空帖

时间:2016-06-09 06:30:17

标签: pagination cakephp-3.0

我正在开发多语言网站。 在网站的英文版本中,所有内容都正常,因为我输入了翻译。

$blogPosts = $this->BlogPosts->find('all')->where(['BlogPosts.active' => 1]);
$this->set('blogPosts',$this->paginate($blogPosts));

如果我更改了网站的语言,我想删除所有未输入标题翻译的帖子。 我试过这个,但它不起作用:

$blogPosts = $this->BlogPosts->find('all')->where(['BlogPosts.active' => 1,'BlogPosts.title IS NOT' => null]);
$this->set('blogPosts',$this->paginate($blogPosts));

仍然没有标题的打印帖子。 如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

尝试以下条件:

public function index()
{
    $this->paginate['conditions'] = ['BlogPosts.active' => true , 'BlogPosts.title IS NOT' => null, 'BlogPosts.title !=' => ' '];
    $this->paginate['order'] = ['BlogPosts.id' => 'desc'];
    $this->paginate['limit'] = 4;

    $blogPosts = $this->paginate($this->BlogPosts);

    $this->set(compact('blogPosts'));
    $this->set('_serialize', ['blogPosts']);

}

查询:

SELECT  *
FROM blogPosts BlogPosts 
WHERE (
    BlogPosts.active = 1 
    AND BlogPosts.title IS NOT NULL 
    AND BlogPosts.title != ''

  ) 
ORDER BY BlogPosts.id desc 
LIMIT 4 OFFSET 0

答案 1 :(得分:0)

我认为以前遇到过simmilar问题。当我下订单时,分页并没有起作用,在条件限制的情况下将其限制在一起。

试试这个:

$this->paginate['order'] = ['BlogPosts.id' => 'desc'];
$this->paginate['limit'] = 4;

$query = $this->BlogPost->find()
->select(['id', 'title', 'active'])
->where(['BlogPost.active' => true, 'BlogPost.title IS NOT' => null, 'BlogPost.id !=' => ''])
$data = $this->paginate($query);
$this->set('BlogPost', $data);