I'm currently developing something in Cakephp 2.7.5. I'm trying to convert some raw sql query to Cakephp style query (using $model->find()
)
I'm stuck at a part where you have to join the results of two sub-queries to the same table. The solution i found was to use joins in cakephp : Joining
But I need to join results of two subqueries and not of two tables. Can this be done in Cakephp?
Query I need:
SELECT
`WorkflowExitDocument`.`app_no`,
`WorkflowExitDocument`.`seq_no`,
`WorkflowExitDocument`.`next_step`
FROM
`ssc`.`workflow_exit_docs` AS `WorkflowExitDocument`
INNER JOIN (
SELECT
APP_NO,
MAX(SEQ_NO) AS SEQ_NO
FROM
workflow_exit_docs
GROUP BY
APP_NO
) AS PREVUSER
ON (
`WorkflowExitDocument`.`app_no` = `PREVUSER`.`APP_NO` AND
`WorkflowExitDocument`.`seq_no` = `PREVUSER`.`seq_no`
)
What I have so far:
$testJoin = $model->find('all', [
'fields' => [
$modelName.'.app_no,
MAX('. $modelName.'.seq_no) AS seq_no'
],
'group' => [
$modelName.'.app_no'
],
'recursive' => -1
]);
$options['fields'] = ['app_no', 'seq_no', 'next_step'];
$options['recursive'] = -1;
$options['joins'] = [
[
'table' => ($testJoin),
'alias' => 'PREVUSER',
'type' => 'INNER',
'conditions' => [
'WorkflowExitDocument.app_no = PREVUSER.APP_NO',
'WorkflowExitDocument.seq_no = MAX(PREVUSER.seq_no)'
]
]
];
$test = $model->find('all', $options);