每个考试都有一个教学大纲,每个考试大纲都有一个考试。所以,我在考试模型中做到了这一点:
public $hasOne = 'Syllabuses'; //table: syllabuses, model: Syllabuses
从UsersController
我这样做了:
public $uses = array('Setting', 'Exam', 'Syllabuses');
在UsersController
的方法中,我想调用paginate:
$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附近使用数组LEFT JOIN
oes
。syllabuses
ASSyllabuses
ON(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
。
答案 0 :(得分: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');