私はCakePHPで衣装のディレクトリウェブサイトを作成しています。私は2つのモデル:Store
とBrand
を持っています。 Brand
はhasAndBelongsToMany
として設定されています。店舗には多くのブランドが関連付けられている可能性がありますが、明らかにブランドは多数の店舗に適用できます。hasAndBelongsToManyとして設定されているモデルを検索
私は自分のStoresController
店舗を持っていますが、私が問題を抱えているのは、BrandsController
のブランドリストを取得していることです。 Brand
モデルのStore
モデルとhasAndBelongsToMany
の関係を設定した場合、もはや$this->Brand->find('all')
のようなことはできませんか?私はとき、私は次のエラーを取得する:ここで
Fatal error: Call to undefined method Brand::find() in /[path]/app/controllers/brands_controller.php on line 8
は私のモデルの定義です:
class Brand {
var $hasAndBelongsToMany = array(
'Store' => array(
'className' => 'Store',
'joinTable' => 'brands_stores',
'foreignKey' => 'brand_id',
'associationForeignKey' => 'store_id',
'unique' => true
)
);
}
そして、私のStore
モデル:
class Store extends AppModel {
var $hasMany = array(
'Reviews' => array(
'className' => 'StoreReview',
'foreignKey' => 'store_id',
'conditions' => array(
'approved' => 1
),
'order' => 'created DESC'
)
);
}
D'oh!ありがとう!あなたが深夜のコードを書くとどうなるのですか? –