2011-07-27 18 views
2

私はテーブルAとデータベースを持っています。フォームを使用してテーブルAfield(let、A.promocode)のデータを編集し、このフォームに新しい入力フィールドを追加し、このフィールドに新しいデータを追加するオプションもあります。useridを使用してください。CAKEPHPデータベースの更新と新しいエントリの追加

今どのように私はそれらに編集promocodeエントリを更新し、また、特定のuseridのためにテーブルに新しいpromocodeエントリを追加することができます。

助けてください。

ありがとうございました。

答えて

0

私は最初に編集を登録して新しい項目を追加することでこれに取り組んでいます。

まずテーブル "A"は非常にひどい例です... cakephpのテーブルでは、常に複数の名前が付けられます。しかし、とにかくあなたのコントローラの上部には、あなたが持っています

$uses = array("A"); <-- This is you model 

私はこのような何かをします。

//First register the update 
$this->A->id = $id //$id you will have to somehow post it with form maybe some hidden field 
$this->A->set("promocode", $_POST["promocode"]); 
$this->A->save(); 
$this->A->id = null; 

//Then register the new entries 
foreach($newEntries as $newEntry) { 
    $this->A->set("promocode", $newEntry["value"]); 
    $this->A->set("user_id", $userid); 
    $this->A->save(); 
    $this->A->id = null; 
} 
0

わからないが、それはあなたの現在のフィールド と、いくつかの動的なフィールド

はA-> SAVEALL(の$ this - >データ)データ使用を節約するために、ここで

を助けたことを願っています

フィールドのIDは ですが、IDのないフィールドが作成されます

<!-- current value--> 
<input type="" name="data[A][edited_id][id]" value="edited_id"/> 
<input type="" name="data[A][edited_id][promocode]" value="edited value"/> 
<input type="" name="data[A][edited_id][user_id]" value="here user id"/> 

<!-- new values--> 
<input type="" name="data[A][1][promocode]" value="new value 1"/> 
<input type="" name="data[A][1][user_id]" value="here user id"/> 

<input type="" name="data[A][2][promocode]" value="new value 2"/> 
<input type="" name="data[A][2][user_id]" value="here user id"/> 

<input type="" name="data[A][3][promocode]" value="new value 3"/> 
<input type="" name="data[A][3][user_id]" value="here user id"/> 
0

これは、DBに物事を保存する正しい方法です、ここに少し例があります

//set some data (important, don't set the 'id') 
$this->data['Workout']['user_id'] = $user_id; 
$this->data['Workout']['name'] = "Ab Blaster"; 

//create workout then save it 
$this->Workout->create(); 
$this->Workout->save($this->data); 
関連する問題