I need to assign hospitals to a group. I've decided to do this via habtm.
class FavoriteHospital extends AppModel {
public $actsAs = array('Containable');
...
var $hasAndBelongsToMany = array(
'Hospital' => array(
'className' => 'Hospital',
'joinTable' => 'favorite_hospitals_hospital',
'foreignKey' => 'favorite_hospital_id',
'associationForeignKey' => 'hospital_id',
'unique' => false,
)
);
}
There is a requirement to limit the number of hospitals in a group to 10. So I set my association to false because the default behaviour just deletes associations. I decided to start by testing the ability to add a hospital using a method....
public function addHospital($gid, $hid) {
$this->data['Hospital']['id'] = $hid;
$this->data['FavoriteHospital']['id'] = $gid;
if($this->save($this->data)){return true;}
return false;
}
Now, the above adds associations correctly, which is great, but every time I try to find a way to count the number of existing associations, I hit a brick wall.
There is a secondary issue with the above, and that is that it also allows duplicates.