I have two models in my CakePHP app: Company and User. They are linked with a HABTM relationship. This creates a “like”, so if a User is linked to a Company then that user likes that company.
How, if viewing a company profile page, can I check if the logged-in user likes the company? I fetch the company details in my controller action…
<?php
class CompaniesController {
public function view($slug) {
$company = $this->Company->findBySlug($slug);
$this->set(array(
'company' => $company,
'is_fan' => ($this->Auth->loggedIn()) ? $this->Company->isFan($this->Auth->user('id')) : false
));
}
}
My thoughts were to have an isFan() method in my Company model—as you can see above—that would take a user ID as a parameter. But I’m getting stuck as to what I’d actually put in this method.
Basically, you query the HABTM join table to determine if the connection exists.