1

Cakephp を使い始めましたが、解決できない問題に遭遇しました。

関連する次のモデル関係があります。

演習 hasMany Points Student hasMany Points,

今度は、すべての演習にポイント データ セットがあるかどうか、studentsController をチェックインし、新しいデータ セットを挿入しないようにしたいと考えています。

ポイント データセットがない状態で開始すると、関数は最初の正しい演習用に新しいポイント データセットを追加しますが、その後、新しいデータセットを作成する代わりに、このデータセットの erxercise_id のみを更新します。

コントローラー関数は次のようになります。

public function correct($id = null) {
    $this->Student->id = $id;
    $this->Student->recursive = 2;
    if ($this->request->is('get')) {
        $data = $this->Student->Exam->Exercise->find('all');
        foreach($data as $exercise)
        {
            $exerciseID = $exercise['Exercise']['id'];
            $this->Student->Point->recursive = 0;
            $foundPoints = $this->Student->Point->find('all', array('conditions' => array('exercise_id' => $exerciseID)));
            if($foundPoints == null)
            {
                $emptyPoints = array ('Point' => array(
                    'exercise_id' => $exerciseID,
                    'student_id' => $id
                    )
                );
                $this->Student->Point->save($emptyPoints);
            }
            else{
            }
        }

    }
    else //POST
    {

    }
}
4

2 に答える 2

2

データを挿入する必要がある場合は、次のように create() メソッドを使用します。これは単なる例です。この行を使用すると、データベースに新しいレコードを作成してデータを保存するたびに

$this->Student->Point->create();
$this->Student->Point->save($emptyPoints);
于 2012-09-04T15:12:50.713 に答える
0
$this->Student->Point->id = '';
$this->Student->Point->save($emptyPoints);
or

 $this->Student->Point->saveAll($emptyPoints);
于 2012-09-04T15:34:11.343 に答える