一个简单条件搜索例子

    public function dashboardmad_index($media_ad_id = null) {
        if (!$media_ad_id)
            $this->redirect('/MediaAds');
        
        $platform='media';
        $this->loadModel('MediaApp');
        $media_app = $this->MediaApp->findByMediaAdId($media_ad_id);
        $this->set(compact('media_app'));
        $this->loadModel('MerchantProductOrder');
        $this->loadModel('Merchant');

        $conditions = array(
            'Merchant.platform' => $platform,
            'MediaAppOrder.media_app_id' => $media_ad_id,
            'OR' => array(
                'Merchant.user_id' => $this->UserAuth->getUserId(),
                'Merchant.self_user_id' => $this->UserAuth->getUserId(),
            )
        );
        $searchInfo = $this->request->query;
        $this->request->data = array('MerchantProductOrder' => $searchInfo);

        if (@$searchInfo['type'] && $searchInfo['keyword']) {
            if ($searchInfo['type'] == 'MerchantProduct.product_name')
                $conditions[$searchInfo['type'] . ' like '] = '%' . $searchInfo['keyword'] . '%';
            else
                $conditions[$searchInfo['type']] = $searchInfo['keyword'];
        }
        if (@$searchInfo['start_time'] && @$searchInfo['end_time']) {
            $conditions['MerchantProduct.start_time >= '] = date('Y-m-d H:m:s',strtotime($searchInfo['start_time']));
            $conditions['MerchantProduct.end_time <= '] = date('Y-m-d H:m:s',strtotime($searchInfo['end_time'])+3600*24);
        }
        if (@$searchInfo['active'] != NULL) {
            if (@$searchInfo['active'] == '-1') {
                $conditions['MerchantProductOrder.active'] = -1;
            } else {
                $conditions['MerchantProductOrder.active'] = 1;
                if (@$searchInfo['active'] == '0')
                    $conditions['MerchantProductOrder.voucher_remaining'] = 0;
                else
                    $conditions['MerchantProductOrder.voucher_remaining >'] = 0;
            }
        }

        $this->paginate = array(
            'limit' => 25,
            'order' => array('MerchantProductOrder.id' => 'desc'),
            'fields' => array(
                'MerchantProductOrder.*',
                'MerchantProduct.*',
                'Merchant.*'
            ),
            'joins' => array(
                array(
                    'table' => 'media_app_order',
                    'alias' => 'MediaAppOrder',
                    'type' => 'inner',
                    'conditions' => array(
                        'MediaAppOrder.merchant_product_order_id = MerchantProductOrder.id',
                    )
                ),
                array(
                    'table' => 'merchant_products',
                    'alias' => 'MerchantProduct',
                    'type' => 'inner',
                    'conditions' => array(
                        'MerchantProduct.id = MerchantProductOrder.product_id',
                    )
                ),
                array(
                    'table' => 'merchants',
                    'alias' => 'Merchant',
                    'type' => 'inner',
                    'conditions' => array(
                        'Merchant.id = MerchantProduct.merchant_id',
                    )
                ),
            ),
            'conditions' => $conditions,
        );
        $this->MerchantProductOrder->recursive = -1;
        $this->MerchantProductOrder->cache = 0;
        $data = $this->paginate('MerchantProductOrder');
        $voucherTypeArr = $this->MerchantProductOrder->voucherTypeArr;
        $ActiveArr = $this->MerchantProductOrder->ActiveArr;
        $platformarr = $this->Merchant->platformSimpleArr;
        foreach ($data as $key => $value) {
            if ($data[$key]['MerchantProductOrder']['voucher_remaining'] == 0 && $data[$key]['MerchantProductOrder']['active']) {
                $data[$key]['MerchantProductOrder']['active'] = 2;
            }
            $data[$key]['MerchantProductOrder']['voucher_type'] = $voucherTypeArr[$data[$key]['MerchantProductOrder']['voucher_type']];
            $data[$key]['MerchantProductOrder']['active_number'] = $data[$key]['MerchantProductOrder']['active'];
            $data[$key]['MerchantProductOrder']['active'] = $ActiveArr[$data[$key]['MerchantProductOrder']['active']];
            $data[$key]['Merchant']['platform_source'] = $data[$key]['Merchant']['platform'];
            $data[$key]['Merchant']['platform'] = @$platformarr[$data[$key]['Merchant']['platform']];
        }

        if (@$searchInfo['export']) {
            $this->orderExport($data);
        }

        $this->set('data', $data);
        $this->set('ActiveArr', $ActiveArr);
        if (!$this->viewVars['data']) {
            $this->warning("没有数据");
        }
        
    }
原文地址:https://www.cnblogs.com/linksgo2011/p/2997265.html