Find on a model that's set as hasAndBelongsToMany
I'm creating a clothing directory website wi开发者_如何学Cth CakePHP. I have two models: Store
and Brand
. Brand
is set up as a hasAndBelongsToMany
, as a store can have many brands associated with it, but obviously a brand can apply to numerous stores.
I have my StoresController
fetching stores as it should, but what I'm having trouble with is fetching a list of brands in my BrandsController
. If I set my Brand
model to have a hasAndBelongsToMany
relationship with my Store
model, can I no longer do something like $this->Brand->find('all')
? When I do I get the following error:
Fatal error: Call to undefined method Brand::find() in /[path]/app/controllers/brands_controller.php on line 8
Here are my model definitions:
class Brand {
var $hasAndBelongsToMany = array(
'Store' => array(
'className' => 'Store',
'joinTable' => 'brands_stores',
'foreignKey' => 'brand_id',
'associationForeignKey' => 'store_id',
'unique' => true
)
);
}
And my Store
model:
class Store extends AppModel {
var $hasMany = array(
'Reviews' => array(
'className' => 'StoreReview',
'foreignKey' => 'store_id',
'conditions' => array(
'approved' => 1
),
'order' => 'created DESC'
)
);
}
class Brand extends AppModel {
Class Brand must extend AppModel, which you seem to have forgotten
精彩评论