0

私のモデルには次のような状況があります。

-a "users" table, with "id" as primary key
-a "stores" table with "id" as primary key, and "users_id" as foreign key
-a "promos" table with "id" as primary key, and "stores_id" as foreign key

つまり、各プロモーションは 1 つのストアのみに属し、各ストアは 1 人のユーザーのみに属し、関係はモデル ファイルに設定されます。

ログインしたユーザーが自分のプロモーションのみを表示できるようにするアクション/ビューを作成したいのですが、方法がわかりません。

「User.id」はプロモーション テーブルの一部ではないため、次のコードではエラー メッセージが表示されますが、このユーザーが所有するストアのみにプロモーション リストを制限するパラメータを作成する方法がわかりません。

$allPromos = $this->Promo->find('all', array('conditions' => array('Store.id' => $storeId, 'Store.enabled' => 1, 'User.id' => $this->Session->read('id'))));

任意のヒント?

ありがとう!

4

2 に答える 2

0

さて、あなたのモデル構造は少し奇妙です。現在、各ストアは 1 人のユーザーのみに属し、各プロモーションは 1 つのストアのみに属しているようです。それが正しい場合は、モデルの関連付けが正しいことを確認してください。ここでユーザーからそれらを見つけているので、 User.php が必要var $hasMany = array('Store')で Store.php が必要ですvar $hasMany = array('Promo')

$allPromos = $this->User->find('all', 
                               array('fields' => array('Promo.*'),
                                     'contain' => array('Store', 'Promo'),
                                     'recursive' => 2 //Two levels of recursion to get from User to Store to Promo
                                     'conditions' => 
                                     array('Store.id' => $storeId, 
                                           'Store.enabled' => 1, 
                                           'User.id' => $this->Session->read('id'))));
于 2013-08-02T23:01:30.770 に答える
0

PromosController で:

$this->Promo->find('all', array(
                          'conditions'=>array(
                             'Store.user_id'=>$this->Session->read('id'),
                             'Store.enabled'=>1),
                          'contain'=>array('Store'));

UsersController で、ユーザー、彼のストア、プロモーションに関する情報を取得します):

$this->User->find('first', array('conditions'=>array(
    'User.id' => $this->Session->read('id'),
    'Store.enabled' => 1),
  'contain'=>array('Store'=>'Promo')
));
于 2013-08-03T00:28:50.593 に答える