2
votes

I have two models: Bar HABTM Foo. Now I have bars_foos join table with some additional fields.

When I add new Bar with some Foo associations or vice versa, I need to run many validation checks, whether the new associations are kosher. Checks are based on the additional fields defined and already saved associations.

Where should I run these validations? In Bar/Foo controller? Or should I create BarsFoo model with validation rules?

When I keep HABTM relationship to get all the auto-magic from Cake, I cannot find a way to tell Cake to use my own predefined BarsFoo model with validations rules. Cake creates its own virtual model and ignores mine.

Or should I (in this specific case) break HABTM into hasMany-belongsTo-hasMany relationship, where I can use my own BarsFoo model?

1

1 Answers

2
votes

You can define your join model using the with key and create your validations there:

public $hasAndBelongsToMany = array(
    'Bar' => array(
        …
        'with' => 'BarFoos'
    )
);

http://book.cakephp.org/view/1044/hasAndBelongsToMany-HABTM