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
                ),
    
            );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP