5

私はcakephp2.0で開発されたサイトを持っています、私は2つのテーブル間の関係を作りたいです:

activity_ingredients

1   id  int(10) UNSIGNED    No  None    AUTO_INCREMENT  
2   type_id     tinyint(2)  No  None        
3   activity_id     int(11)     No  None        
4   ingredient_id   int(10)     No  None        
5   created     datetime        

行動

1   id  int(10) UNSIGNED    No  None    AUTO_INCREMENT  
2   type_id     tinyint(2)  No  None        
3   language    char(2)     No  None        
4   text    varchar(100)        No  None        
5   created     datetime    

2つのテーブルをフィールド「type_id」に関連付けたいと思います。私はこのモードで自分のコードを実行しました:

    class Action extends AppModel{
    public $name = 'Action'; //non utilizzata nel sito è il nome del modello alla fine per migliorare la compatibilità

    public $belongsTo = array(
        'ActivityIngredients' => array(
            'className'     => 'ActivityIngredients',
            'conditions'    => '',
            'order'         => '',
            'foreignKey'    => 'type_id'
        )
    );

}

class ActivityIngredients extends AppModel{
        public $name = 'ActivityIngredients'; //non utilizzata nel sito è il nome del modello alla fine per migliorare la compatibilità

        public $belongsTo = array(
            'Activity' => array(
                'className'     => 'Activity',
                'conditions'    => '',
                'order'         => '',
                'foreignKey'    => 'activity_id'
            ),
            'Ingredient' => array(
                'className'     => 'Ingredient',
                'conditions'    => '',
                'order'         => '',
                'foreignKey'    => 'ingredient_id'
            )
        );

        public $hasMany = array(
            'Action' => array(
                'className' => 'Action',
                'conditions' => '',
                'dependent' => true,
                'foreignKey'   => 'type_id',
                'associatedKey'   => 'type_id'
            )
        );
    }

正しいデータを取得していません。外部キーのIDを取得しているようです。これはビューです:

<?php foreach ($user['Activity'] as $activities) {
var_dump($activities);
?>
    <div class="line-cnt"><div class="line">
    </div>
</div>
<h2>Attività</h2>
<div class="table">
    <div>
        <div>Activity created</div><div><?php echo $activities['created']; ?>
        </div>
    </div>
    <div>
        <div>Actions text</div><div><?php echo $activities['Action']['text']; ?></div>
    </div>
    <div>
        <div>ActivityIngredient ingredient_id</div><div><?php echo $activities['ActivityIngredients']['ingredient_id']; ?></div>
    </div>
</div>
<?php
}
?>

コントローラは、テーブルと連携するユーザーへのすべての再帰3を検索する単純なクエリです。

$this->User->recursive = 3;
        $user = $this->User->read();

        if (empty($username) || $username != $user['User']['username']) {
            $this->redirect(array ('action'=>'view',$id,$user['User']['username']));
        }

        $this->set('user', $user);

お願い助けて

4

2 に答える 2

2

まず、「activity_ingredients」テーブルで「id」フィールドを使用している場合は、それを別のテーブルでforeignKeyとして使用する必要があります。

外部キーは、別のテーブルの候補キーと一致するリレーショナル テーブル内のフィールドです。

「actions」テーブルで type_id を外部キーとして使用しようとしている場合でも、type_id は activity_ingredients テーブルで一意である必要があります。その場合、ActivityIngredient モデルを次のように定義できます。

class ActivityIngredients extends AppModel{
    public $primaryKey = 'type_id';
    public $name = 'ActivityIngredients'; //non utilizzata nel sito è il nome del modello alla fine per migliorare la compatibilità

    public $belongsTo = array(
        'Activity' => array(
            'className'     => 'Activity',
            'conditions'    => '',
            'order'         => '',
            'foreignKey'    => 'activity_id'
        ),
        'Ingredient' => array(
            'className'     => 'Ingredient',
            'conditions'    => '',
            'order'         => '',
            'foreignKey'    => 'ingredient_id'
        )
    );

    public $hasMany = array(
        'Action' => array(
            'className' => 'Action',
            'conditions' => '',
            'dependent' => true,
            'foreignKey'   => 'type_id',
            'associatedKey'   => 'type_id'
        )
    );
}

そしてあなたのアクションモデルは変わりません。したがって、目的のレコードを取得できます。

また、「type_id」をテーブルの外部キーとして定義することに同意しない場合でも。次に、このコードはあなたの状況で非常にうまく機能します。

class ActivityIngredients extends AppModel{
public $name = 'ActivityIngredients'; //non utilizzata nel sito è il nome del modello alla fine per migliorare la compatibilità

public $belongsTo = array(
    'Activity' => array(
        'className'     => 'Activity',
        'conditions'    => '',
        'order'         => '',
        'foreignKey'    => 'activity_id'
    ),
    'Ingredient' => array(
        'className'     => 'Ingredient',
        'conditions'    => '',
        'order'         => '',
        'foreignKey'    => 'ingredient_id'
    )
);

public $hasMany = array(
    'Action' => array(
        'className' => 'Action',
        'conditions' => '',
        'dependent' => true,
        'foreignKey'   => false,
        'finderQuery'   => 'select * from actions as `Action` where
                            `Action`.`type_id` = {$__cakeID__$} '
    )
);

}

これにより、望ましい結果が得られると確信しています。うまくいかない場合は、お気軽にお問い合わせください。

于 2012-07-20T04:41:07.923 に答える
-1

Cakephpでは、モデルの主キーを割り当てることができます。クラス名を外部キーの関連付けに入れることもできると思います。例えば

'foreignKey'   => 'Classname.type_id'
于 2012-07-19T22:52:29.707 に答える