1

每个考试都有一个大纲,每个大纲都有一个考试。所以,我在考试模型中这样做了:

public $hasOne = 'Syllabuses'; //table: syllabuses, model: Syllabuses

UsersController我这样做:

public $uses = array('Setting', 'Exam', 'Syllabuses');

UsersController我想调用分页的方法中:

$options = array(
    'fields' => array('Exam.id', 'Exam.name', 'Syllabuses.id', 'Syllabuses.name', 'Syllabuses.syllabus', 'Syllabuses.last_updated'),
    'joins' => array(
        'table' => 'syllabuses',
        'conditions' => array('Exam.id = Syllabuses.exam_id')
    )
);
$this->paginate = $options;
$this->set('syllabuses', $this->Paginator->paginate('Syllabuses'));

考试表:

---+------+
id | name |
---+------+

和课程表:

---+---------+------+----------+--------------+
id | exam_id | name | syllabus | last_updated |
---+---------+------+----------+--------------+

所以,我得到了一些错误。像这样:

错误:SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取在 'syllabuses Array LEFT JOIN 附近使用的正确语法oessyllabusesAS SyllabusesON ( Syllabuses.`' 在第 1 行

而我的 CakePHP 准备的 SQL 是:

SELECT `Exam`.`id`, `Exam`.`name`, `Syllabuses`.`id`, `Syllabuses`.`name`, `Syllabuses`.`syllabus`, `Syllabuses`.`last_updated` 
FROM `oes`.`exams` AS `Exam` syllabuses Array 
LEFT JOIN `oes`.`syllabuses` AS `Syllabuses` ON (`Syllabuses`.`exam_id` = `Exam`.`id`)
WHERE 1 = 1 LIMIT 20

但我想要的是类似于下面的查询。我在mysql中测试过:

SELECT  `Exam`.`id` AS eid,  `Exam`.`name` AS ename,  `Syllabuses`.`id` AS sid,  `Syllabuses`.`name` AS sname,  `Syllabuses`.`syllabus` ,  `Syllabuses`.`last_updated` 
FROM  `oes`.`syllabuses` AS  `Syllabuses` , exams AS Exam
WHERE Exam.id = Syllabuses.exam_id
ORDER BY Exam.id
LIMIT 20

现在请任何人帮助我实现这一目标。什么样的改变可以让 CakePHP 准备像那样的查询(我尝过的)来完成我的Pagination工作。

4

1 回答 1

2

好的,我认为这对许多程序员都有帮助。这就是为什么我想分享我最后所做的事情:

$options = array(
    'fields' => array(
        'Exam.id',
        'Exam.name',
        'Syllabuses.id',
        'Syllabuses.name',
        'Syllabuses.exam_id',
        'Syllabuses.syllabus',
        'Syllabuses.last_updated'
    ),
    'recursive' => 0,
    'conditions' => array('Exam.id = Syllabuses.exam_id'),
    'limit' => 3
);
$this->paginate = $options;
$syllabuses = $this->Paginator->paginate('Exam');
于 2013-09-26T06:36:03.690 回答