【问题标题】:Cakephp 3 - Remove empty post from paginationCakephp 3 - 从分页中删除空帖子
【发布时间】:2016-06-09 06:30:17
【问题描述】:

我正在开发多语言网站。 在英文版网站一切正常,因为我输入了翻译。

$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));

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

【问题讨论】:

标签: pagination cakephp-3.0


【解决方案1】:

试试这个条件:

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

【讨论】:

  • 它不起作用。执行查询后如何使用分页数组进行操作?执行查询后,我想删除一些帖子
  • 你想用什么条件来删除一些帖子?
  • @user3661042 尝试删除所有$this->paginate['conditions'] 并添加此$this->paginate['conditions'] = ['BlogPosts.active' => true , 'BlogPosts.title IS NOT' => null, 'BlogPosts.title !=' => ' '];
  • @user3661042 我用新查询编辑我的答案,请尝试该代码。如果您想编辑某些条件,只需删除、添加或更改数组条件
  • $this->paginate['conditions'] = ['BlogPosts.active' => true , 'BlogPosts.title IS NOT' => null, 'BlogPosts.title !=' => ' '];..... 这没用。这个条件永远不会满足,因为英文版的网站标题不为空。英文版的每篇文章都包含标题。
【解决方案2】:

我认为以前遇到过类似的问题。分页不起作用,因为我设置顺序时例外,限制与自定义位置条件一起。

试试这个:

$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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-01
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    • 2014-06-09
    • 1970-01-01
    • 1970-01-01
    • 2012-12-10
    相关资源
    最近更新 更多