2016-03-19 67 views
1

你好我有这样的用户模型的关系。CakePHP分页计数查询不起作用

的usermodel

public $hasOne = [ 
     'Userdetail' => [ 
      'className' => 'Userdetail', 
      'foreignKey' => 'user_id' 
     ], 
     'Application' => [ 
      'className' => 'Application', 
      'foreignKey' => 'user_id' 
     ], 
    ]; 

我在下面MyConroller像添加额外的HasOne关系:

控制器

$this->User->bindModel([ 
      'hasOne' => [ 
       'Events' => [ 
        'className' => 'Events', 
        'foreignKey' => 'user_id', 
        'dependent' => true, 
       ] 
      ] 
     ]); 

我创建一个用户模型像下面分页:

分页

$this->paginate = [ 
      'conditions' => [ 
       $this->conditions, 
       'Events.id !=' => null 
      ], 
      'order' => 'User.id DESC' 
     ]; 

我不希望记录,其事件ID为空,所以我把这个分页条件。比我低于错误。

Unknown column 'Events.id' in 'where clause' 

事件的模型不反映在Count分页查询中。

请帮我解决这个问题。

在此先感谢您的帮助。

+0

'$这 - >用户 - > bindModel([ \t 'hasOne'=> [ \t \t '事件'=> [ \t \t \t '的className'=> '事件', \t \t \t 'FOREIGNKEY'=> 'USER_ID', \t \t \t '依赖'=>真, \t \t], \t \t '条件'=> [ '!Events.id ='=>空, \t \t] \t] ]);'和'从分页condition' –

+0

删除它不,它是CakePHP的2.6 –

+0

我试试这个@ Anant,但它不起作用 –

回答

0

您必须添加false作为第二个参数中bindModel,使更改永久:

$this->User->bindModel([ 
     'hasOne' => [ 
      'Events' => [ 
       'className' => 'Events', 
       'foreignKey' => 'user_id', 
       'dependent' => true, 
      ] 
     ] 
    ],false); 

否则CakePHP就会回复到产生计数,这是从不同的第二个查询前面定义的关系第一个检索结果。

参见Model::bindModel()