douyun3887 2015-08-04 06:07
浏览 26
已采纳

CakePHP - 分页查询附加默认条件“TableName.deleted”!= 1“

I'm using Cakephp V2.0 and having huge application working on it. Below is the what issue i'm facing right now.

Issue: Select Query is what automatically set condition like "TableName.deleted" != 1

Which does the creating issue actually, i want to add my custom condition in this query to get all soft-deleted records i.e.: "TableName.deleted" == 1 . but when We are using $this->paginate() function, it will append default condition at the end of MySQL Query condition and then MySQL Query will look like this:

("TableName.deleted" == 1) AND "TableName.deleted" != 1 Order By xyz

So it's retrieving the record set only by taking last condition and return only records which are not deleted.

How do i remove this default CakePHP condition ("TableName.deleted" != 1)?

Edited(Added Code):

if (isset($this->passedArgs['showdeleted']) && $this->passedArgs['showdeleted'] == 1) {
            $displayConditions['AND']['tableName.deleted'] = "1";
        } else {
            $displayConditions['AND']['tableName.deleted'] = "0";
        }

        $this->paginate = array(
            'conditions' => $displayConditions,
            'fields' => $this->displayFields,
            'limit' => $show_page,
            'group' => 'tableName.id',
            'contain' => array(
                'tbl1',
                'tbl2',
                'tbl3',
                'tbl4',
                'tbl5',
            ),
        );

        $returnRecords = $this->paginate();

Can anyone please let me know what should i do to resolve this. any help would be appreciated!

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dousi1961 2015-08-05 08:24
    关注

    Thanks for your suggesting and comments.

    Actually i got the answer for the same. we have created behavior for the same

    SoftDeletableBehavior.php

    In which we have defined related methods which was appending the condition "TableName.deleted" != 1. and it was also mentioned in Model as well like below:

    var $actsAs = array(
            'SoftDeletable' => array(
                'field' => 'deleted',
                'find' => true
            ),
    
        );
    

    so i just made one very minor change and it's working.

    var $actsAs = array(
                'SoftDeletable' => array(
                    'field' => 'deleted',
                    'find' => false
                ),
    
            );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历