Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting CakePHP-paginate和sort有许多关联_Sorting_Cakephp_Paginator - Fatal编程技术网

Sorting CakePHP-paginate和sort有许多关联

Sorting CakePHP-paginate和sort有许多关联,sorting,cakephp,paginator,Sorting,Cakephp,Paginator,我有三张桌子: 书籍->属于->文档,文档->有一本->书籍,文档->有许多->语言,语言->属于->文档 Document.php public $hasOne = array( 'Book' => array( 'className' => 'Book', 'foreignKey' => 'document_id', 'dependent'=>true, ) public $hasMany = arra

我有三张桌子:

书籍->属于->文档,文档->有一本->书籍,文档->有许多->语言,语言->属于->文档

Document.php

public $hasOne = array(
    'Book' => array(
        'className' => 'Book',
        'foreignKey' => 'document_id',
        'dependent'=>true,
    )

public $hasMany = array(
    'Language' => array(
        'className' => 'Language',
        'foreignKey' => 'document_id'
    )
public $belongsTo = array(
    'Document' => array(
        'className' => 'Document',
        'foreignKey' => 'document_id'
    )
);
public $belongsTo = array(
    'Document' => array(
        'className' => 'Document',
        'foreignKey' => 'document_id'
    )
);
    $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());
            <th><?php echo $this->Paginator->sort('Document.Language.type',__('language')); ?></th>
Book.php

public $hasOne = array(
    'Book' => array(
        'className' => 'Book',
        'foreignKey' => 'document_id',
        'dependent'=>true,
    )

public $hasMany = array(
    'Language' => array(
        'className' => 'Language',
        'foreignKey' => 'document_id'
    )
public $belongsTo = array(
    'Document' => array(
        'className' => 'Document',
        'foreignKey' => 'document_id'
    )
);
public $belongsTo = array(
    'Document' => array(
        'className' => 'Document',
        'foreignKey' => 'document_id'
    )
);
    $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());
            <th><?php echo $this->Paginator->sort('Document.Language.type',__('language')); ?></th>
Language.php

public $hasOne = array(
    'Book' => array(
        'className' => 'Book',
        'foreignKey' => 'document_id',
        'dependent'=>true,
    )

public $hasMany = array(
    'Language' => array(
        'className' => 'Language',
        'foreignKey' => 'document_id'
    )
public $belongsTo = array(
    'Document' => array(
        'className' => 'Document',
        'foreignKey' => 'document_id'
    )
);
public $belongsTo = array(
    'Document' => array(
        'className' => 'Document',
        'foreignKey' => 'document_id'
    )
);
    $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());
            <th><?php echo $this->Paginator->sort('Document.Language.type',__('language')); ?></th>
BooksController.php

public $hasOne = array(
    'Book' => array(
        'className' => 'Book',
        'foreignKey' => 'document_id',
        'dependent'=>true,
    )

public $hasMany = array(
    'Language' => array(
        'className' => 'Language',
        'foreignKey' => 'document_id'
    )
public $belongsTo = array(
    'Document' => array(
        'className' => 'Document',
        'foreignKey' => 'document_id'
    )
);
public $belongsTo = array(
    'Document' => array(
        'className' => 'Document',
        'foreignKey' => 'document_id'
    )
);
    $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());
            <th><?php echo $this->Paginator->sort('Document.Language.type',__('language')); ?></th>
index.php

public $hasOne = array(
    'Book' => array(
        'className' => 'Book',
        'foreignKey' => 'document_id',
        'dependent'=>true,
    )

public $hasMany = array(
    'Language' => array(
        'className' => 'Language',
        'foreignKey' => 'document_id'
    )
public $belongsTo = array(
    'Document' => array(
        'className' => 'Document',
        'foreignKey' => 'document_id'
    )
);
public $belongsTo = array(
    'Document' => array(
        'className' => 'Document',
        'foreignKey' => 'document_id'
    )
);
    $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());
            <th><?php echo $this->Paginator->sort('Document.Language.type',__('language')); ?></th>

我就是不能按语言类型对数据排序


有人可以建议解决此问题吗?

您可以使用虚拟字段功能按语言类型排序

首先,将此虚拟字段添加到文档模型中:

public $virtualFields = ['Min_LangType' => 'SELECT MIN(type) FROM languages as Language WHERE Language.document_id = Document.id'];
然后,按语言类型按以下方式排序:

$this->Paginator->sort('Document.Min_LangType',__('language'));

非常感谢你!您的虚拟字段是Min_LangType,因此您应该在index.php中使用Document.Min_LangType。我不敢相信答案这么简单!