Skip to content

@martinbean /NewsArticle.php
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching records from one table based on existence of records in a HABTM relation.
<?php
class NewsArticle extends AppModel {
public $name = 'NewsArticle';
public $hasAndBelongsToMany = array(
'Image' => array(
'joinTable' => 'news_articles_images'
)
);
public function findWithImage($type, $options = array()) {
$options['joins'] = array(
array(
'table' => 'news_articles_images',
'alias' => 'NewsArticlesImage',
'type' => 'inner',
'conditions' => array(
'NewsArticle.id = NewsArticlesImage.news_article_id'
)
),
array(
'table' => 'images',
'alias' => 'Image',
'type' => 'inner',
'conditions' => array(
'NewsArticlesImage.image_id = Image.id'
)
)
);
return $this->find($type, $options);
}
}
<?php
class NewsArticlesController extends AppController {
public function home() {
$topStory = $this->NewsArticle->findWithImage('first');
$this->set('topStory', $topStory);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.