0

ユーザーとアーティストの 2 つのテーブルがあります。

User:
id
name
password
city_id
state_id
country_id

Artist:
id
user_id
description

追加および編集フォームを作成したい (単一のフォームが追加および編集として機能する)。しかし、編集リンクをクリックすると、ユーザーとアーティストテーブルのフォームフィールドに入力したいと思います。それ、どうやったら出来るの?以下は私のコントローラーコードです:

public function artist_register() {
        $this->country_list();
        if ($this->request->is('post')) {
            $this->request->data['User']['group_id'] = 2;
            if ($this->{$this->modelClass}->saveAll($this->request->data)) {
                $this->redirect(array('controller' => 'Users', 'action' => 'login'));
            }
        }
        $this->render('artist_update');
    }

    public function artist_update($id) {
        $artist = $this->{$this->modelClass}->findByuser_id($id);
        $artist_id = $artist[$this->modelClass]['id'];

        if($this->request->is('get')) {
            $this->request->data = $this->{$this->modelClass}->read(null, $artist_id);
            $this->request->data = $this->{$this->modelClass}->User->read(null, $id);
        } else {
            if($this->{$this->modelClass}->save($this->request->data)) {
                $this->Session->setFlash('Your Profile has been updated');
                $this->redirect(array('action' => 'index'));
            }
        }
    }

コードが機能しません。どうすれば解決できますか?

4

1 に答える 1

1

最初に行うこと:

と の間の関係を定義する必要がUserありArtistます。

A user has one artist
A user has many artist
A artist belongs to an user

この場合、あなたが探している関係は次のとおりだと思います。

A artist belongsTo user

コーディング時間:

これがわかれば、関係を定義できるようになります。モデルでは、変数Artistを宣言する必要があります。belongsTo

class Artist extends AppModel {
    public $belongsTo = 'User';
}

または、次のように関係をセットアップできます。

class Artist extends AppModel {
    public $belongsTo = array(
        'User' => array(
            'className'    => 'User',
            'foreignKey'   => 'user_id'
        )
    );
}

ご参考までに:

belongsTo関連付けで使用できる別のキーがあります。

  • className : 現在のモデルに関連付けられているモデルのクラス名。「アーティストがユーザーに属する」関係を定義している場合、className キーは「ユーザー」に等しい必要があります。</p>

  • foreignKey : 現在のモデルで見つかった外部キーの名前。これは、複数の belongsTo 関係を定義する必要がある場合に特に便利です。このキーのデフォルト値は、下線付きの他のモデルの単数形の名前で、_id のサフィックスが付いています。

  • conditions : find() 互換条件の配列、または array('User.active' => true) などの SQL 文字列

  • type : SQL クエリで使用する結合のタイプ。デフォルトは LEFT で、すべての状況でニーズに合わない可能性があります。INNER は、メイン モデルと関連モデルのすべてが必要な場合、または何も必要ない場合に役立ちます。(もちろん条件付きで使用すると効果的です)。(注意: 型の値は小文字です。つまり、左、内側)

  • fields : 関連するモデル データがフェッチされるときに取得されるフィールドのリスト。デフォルトですべてのフィールドを返します。

  • order : find() 互換の order 句または array('User.username' => 'ASC') などの SQL 文字列の配列

何を期待します?

これは、$this->Artist->find()関係を定義した後の呼び出しの結果です。

Array
(
    [Artist] => Array
    (
        [id] => 5
        [user_id] => 1
        [description] => I am an artist!
    )
    [User] => Array
    (
        [id] => 1
        [name] => Fuhrmann
        [passsword] => 1234
        [city_id] => 500
        [state_id] => 2020
        [country_id] => 223
    )

)

だから何?

Userこのすべての後、を探しているときに にアクセスできますArtist

public function artist_update($id) {
    // Read the artist and the user info
    $artist = $this->{$this->modelClass}->findByUserId($id);        
    $this->set(compact('artist'));
}

Usersビューでは、ユーザーの名前またはテーブル内の別のフィールドを取得できます。

<input type="text" name="name" value="<?php echo $artist['User']['name'];?>"
<input type="text" name="user_id" value="<?php echo $artist['User']['id'];?>"
<input type="text" name="country_id" value="<?php echo $artist['User']['country_id'];?>"

続きを読む

アソシエーションの詳細: モデルをリンクする

于 2012-11-03T16:57:10.573 に答える