cakePHP 2.0 App的基本搜索功能 - 注意事项(8):未定义的索引:搜索错误

时间:2014-10-07 22:30:30

标签: php cakephp

嗨,我是带有cakePHP的newB,我很享受,但是有一种严峻的学习曲线......

无论如何,我试图在视图中设置一个基本搜索,只搜索一个表的结果。这是一个cakePHP 2.0应用程序。我收到了可怕的通知(8):未定义的索引:搜索错误。

其他一切正常。

非常感谢任何帮助。

这是我的CustomersController代码

/**
 * index method
 *
 * @return void
 */
    public function index() {
        $this->layout = 'front';
        //  $this->Paginator->settings = $this->paginate;
        $this->Paginator->settings = array(
            'Customer' => array(
                'paramType' => 'querystring',
                'limit' => 5,
                'order' => array('Customer.created' => 'desc'
                )
            )
        );

        $this->Customer->recursive = 0;
        $this->set('customers', $this->Paginator->paginate());


/**
 *  Search Method
 *
 *
 */ 
    public function search() {
      if ($this->request->is('put') || $this->request->is('post')) {
        // poor man's Post Redirect Get behavior
        return $this->redirect(array(
          '?' => array(
            'q' => $this->request->data('Customer.searchQuery')
          )
        ));
      }
      $this->Customer->recursive = 0;
      $searchQuery = $this->request->query('q');
      $this->Paginator->settings = array(
        'Customer' => array(
          'findType' => 'search',
          'searchQuery' => $searchQuery
        )
      );
      $this->set('customers', $this->Paginator->paginate());
      $this->set('searchQuery', $searchQuery);
      $this->render('index');
    }

这是我的客户模型

/**
 * customer Model
 *
 */
class customer extends AppModel {

    public $findMethods = array('search' => true);

    protected function _findSearch($state, $query, $results = array()) {
      if ($state === 'before') {
        $searchQuery = Hash::get($query, 'searchQuery');
        $searchConditions = array(
          'or' => array(
            "{$this->alias}.name LIKE" => '%' . $searchQuery . '%',
            "{$this->alias}.interest LIKE" => '%' . $searchQuery . '%'
          )
        );
        $query['conditions'] = array_merge($searchConditions, (array)$query['conditions']);
        return $query;
      }
      return $results;
    }

再次感谢任何帮助。提前谢谢!

0 个答案:

没有答案