1

ユーザー、ゲーム、およびゲーム プラットフォーム (PS3 用) を使用して Cake php アプリケーションを作成したいのですが、テーブルを取得しました。

userGames (ゲームには 1 つのプラットフォームがあります) id、name、platform_id

ユーザー (ユーザーは多くのプラットフォームを持つことができます) id、ユーザー名

プラットフォーム ID、名前

users_platforms id、platform_id、user_id

そして今、すべてのユーザー id=1 プラットフォームを選択し、選択タグにリストします。SQLクエリは次のとおりです。

SELECT platforms.name, platforms.id FROM platforms LEFT JOIN platforms_users ON platforms_users.platform_id=platforms.id WHERE platforms_users.user_id=1

しかし、cakePHP の find('list') 関数で何をリストすればよいかわかりません。ユーザー コントローラーに次のように入力してみます。

$this->User->Platform->find('list', array('conditions', array('User.id'=>'1')));

しかし、これはSQLの問題を返します(未定義のUser.id)誰でも私を助けることができますか?

4

3 に答える 3

3

これを試してください

$this->loadModel('UserPlatform');
$this->UserPlatform->bindModel(array(
    'belongsTo' => array('Platform')
));
$this->UserPlatform->find('list', array(
    'fields' => array('Platform.id','Platform.name'),
    'conditions' => array('UserPlatform.user_id'=>'1'),
    'recursive' => 1
));
于 2012-08-18T14:51:17.080 に答える
0

you must apply find function on join table

Your find must same as this:

 $this->PlatformUser->find('list',array('fields'=>
 array('Platform.name','Platform.id'),'conditions'=> array('PlatformUser.id' => 1)));

Your PlatformUser Model must have:

public $belongsTo = array(
    'Platform' => array(
        'className' => 'Platform',
        'foreignKey' => 'platform_id',
    ),
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'user_id',
    ),
);
于 2012-08-18T16:47:43.403 に答える
0

あなたがやろうとしているのは、実際には HasAndBelongsToMany アソシエーションです。

http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html

hasAndBelongsToMany (HABTM)

HABTM 関連付けを処理するには、データベースに追加のテーブルを設定する必要があります。この新しい結合テーブルの名前には、関連する両方のモデルの名前をアルファベット順に含め、アンダースコア ( _ ) で区切る必要があります。テーブルの内容は、関係するモデルの主キーを指す外部キー (整数である必要があります) である 2 つのフィールドである必要があります。問題を回避するには、これら 2 つのフィールドに結合された主キーを定義しないでください。アプリケーションで一意のインデックスが必要な場合は、それを定義できます。このテーブルに追加情報を追加する予定がある場合、または「with」モデルを使用する場合は、追加の主キー フィールドを追加する必要があります (慣例により「id」)。

HABTM には、両方のモデル名を含む別個の結合テーブルが必要です。

(これが正しい場合、これは UserPlatform テーブルになります。)

テーブル Cakes と Recipe の主キーには、慣例で想定されている「id」フィールドがあることを確認してください。想定と異なる場合は、モデルの primaryKey で変更する必要があります。

class Recipe extends AppModel {
public $hasAndBelongsToMany = array(
    'Ingredient' =>
        array(
            'className' => 'Ingredient',
            'joinTable' => 'ingredients_recipes',
            'foreignKey' => 'recipe_id',
            'associationForeignKey' => 'ingredient_id',
            'unique' => true,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
            'with' => ''
        )
);
}
于 2014-09-09T10:22:36.757 に答える