CakePHP-分页和排序hasMany关联 [英] CakePHP - paginate and sort hasMany association
本文介绍了CakePHP-分页和排序hasMany关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有三个表:
图书-> 属于->文档,文档-> 有一个->图书,文档-> 有很多->语言,语言- > 属于->文档
Book -> belongTo -> Document ,Document -> hasOne -> Book, Document -> hasMany -> Language , Language -> belongTo -> Document
Document.php
public $hasOne = array(
'Book' => array(
'className' => 'Book',
'foreignKey' => 'document_id',
'dependent'=>true,
)
public $hasMany = array(
'Language' => array(
'className' => 'Language',
'foreignKey' => 'document_id'
)
Book.php
public $belongsTo = array(
'Document' => array(
'className' => 'Document',
'foreignKey' => 'document_id'
)
);
Language.php
public $belongsTo = array(
'Document' => array(
'className' => 'Document',
'foreignKey' => 'document_id'
)
);
BooksController.php
$this->Book->bindModel(array(
'hasOne' => array(
'Language' => array(
'foreignKey' => false,
'conditions' => array('Document.id = Language.document_id')
));
$this->Prg->commonProcess();
$this->Paginator->settings = [
'conditions' => $this->Book->parseCriteria($this->Prg->parsedParams()),
'contain' => array('Document','Language')
];
$this->set('books', $this->Paginator->paginate());
index.php
<th><?php echo $this->Paginator->sort('Document.Language.type',__('language')); ?></th>
我只是无法按语言类型对数据进行排序!
I just can't sort data by language type!
任何人都可以建议解决此问题的方法吗?
Can anyone suggest a fix for this problem?
推荐答案
您可以使用虚拟字段功能来按语言类型进行排序.
You can use the virtual field feature in order to sort by language type.
首先,将此虚拟字段添加到您的文档模型中:
First, add this virtual field to your Document model:
public $virtualFields = ['Min_LangType' => 'SELECT MIN(type) FROM languages as Language WHERE Language.document_id = Document.id'];
然后,以这种方式按语言类型排序:
Then, sort by language type in this way:
$this->Paginator->sort('Document.Min_LangType',__('language'));
这篇关于CakePHP-分页和排序hasMany关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文