My cakephp app (2.2) app has the following:
NewsArticle HMBTM NewsCategory
NewsCategory HMBTM NewsArticle
In my News article controller function index() I'm trying to get a paginated list of news articles that have the news category id 2.
Heres my code (which is wrong):
$this->paginate = array(
'conditions' => array('newsArticle.news_category_id = 2'),
'limit' => 10,
'order' => array(
'newsArticle.modified' => 'asc'
)
);
$this->set('newsArticles', $this->paginate());
Could someone tell me where I'm going wrong? I'm guessing this is to do with the join table.
Heres the error I'm getting:
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'newsArticle.news_category_id' in 'where clause'
Heres the SQL its producing:
SQL Query:
SELECT
`NewsArticle`.`id`,
`NewsArticle`.`title`,
`NewsArticle`.`brief`,
`NewsArticle`.`body`,
`NewsArticle`.`filename`,
`NewsArticle`.`dir`,
`NewsArticle`.`mimetype`,
`NewsArticle`.`filesize`,
`NewsArticle`.`live`,
`NewsArticle`.`user_id`,
`NewsArticle`.`created`,
`NewsArticle`.`modified`,
`User`.`id`,
`User`.`username`,
`User`.`password`,
`User`.`forename`,
`User`.`surname`,
`User`.`company`,
`User`.`position`,
`User`.`role`,
`User`.`version_numbers_id`,
`User`.`support_case_reference`,
`User`.`support_web_password`,
`User`.`webforms_email`,
`User`.`tech_email`,
`User`.`group_id`,
`User`.`user_status_id`,
`User`.`view_uat`,
`User`.`manuals`,
`User`.`filename`,
`User`.`dir`,
`User`.`mimetype`,
`User`.`filesize`,
`User`.`created`,
`User`.`modified`,
`User`.`live`,
`User`.`tokenhash`
FROM `cakeclientarea`.`news_articles` AS `NewsArticle`
LEFT JOIN `cakeclientarea`.`users` AS `User`
ON (`NewsArticle`.`user_id` = `User`.`id`)
WHERE
`newsArticle`.`news_category_id` = 2
ORDER BY
`newsArticle`.`modified` asc
LIMIT 10
I can see from this it's not even touching the join table news_articles_news_categories.
Can anyone help, surely this is fairly simple to do? What am I missing? Thanks in advance.