I am using cakephp 2.1 and written sql statement as follows.
SELECT * FROM industry
LEFT JOIN movie ON movie.industry_id = industry.id
LEFT JOIN (
SELECT MAX(id) AS TID ,movie_id FROM trailer
GROUP BY movie_id
) AS c ON c.movie_id = movie.id
LIMIT 0, 4;
where industry is “Industry” model, movie is “Movie” model and trailer is “Trailer” model and I have tried this one.
$options['joins'] = array(
array(
'table' => 'movies',
'alias' => 'Movie',
'type' => 'left',
'conditions' => array('Industry.id = Movie.industry_id')),
array(
'table' => 'movie_trailers',
'alias' => 'Trailer',
'type' => 'left',
'conditions' => array('Trailer.movie_id = Movie.id')));
$trailers = $this->Industry->find('all', $options);
So please suggest me to convert this statement to cakephp statement.
Your SQL is a
joinwith asub query, while the Cake code isjoinonly.You can do a sub query but I would look for a way to do it with a join only.
Given the lack of details in your question I cant help with a better query.