2012-05-03 18 views
0

私はcakePHPを使用していますが、名前がテーブルに挿入されない理由は、テーブルが更新されたときに値が継承されるためです。CakePHP DBフィールドの更新に失敗しました

データベースのテーブル

CREATE TABLE tests (
id serial not null unique primary key, 
name varchar, 
created timestamp default CURRENT_TIMESTAMP 

) 

add.ctp

<?php 
echo $this->Form->create('Post'); 
echo $this->Form->input('name'); 
echo $this->Form->end('RSVP'); 
?> 

試験モデル

<?php 
class Test extends AppModel { 
var $name = 'Test'; 
} 

TestContoller

<?php 
class TestController extends AppController { 
public $helpers = array('Html', 'Form'); 

var $name = "Test"; 


public function index() { 
    $this->set('tests', $this->Test->find('all')); 
} 

public function view($id = null) { 
    $this->Test->id = $id; 
    $this->set('Test', $this->Test->read()); 
} 

public function add() { 
    if ($this->request->is('post')) { 
     if ($this->Test->save($this->request->data)) { 
      $this->Session->setFlash('Your post has been saved.'); 
      $this->redirect(array('action' => 'index')); 
     } 
     else { 
      $this->Session->setFlash('Unable to add your post.'); 
     } 
    } 
} 

public function edit($id = null) { 
    $this->Test->id = $id; 
    if ($this->request->is('get')) { 
     $this->request->data = $this->Post->read(); 
    } 
    else { 
     if ($this->Test->save($this->request->data)) { 
      $this->Session->setFlash('Your post has been updated.'); 
      $this->redirect(array('action' => 'index')); 
     } 
     else { 
      $this->Session->setFlash('Unable to update your post.'); 
     } 
    } 
} 

public function delete($id) { 
     if ($this->request->is('get')) { 
      throw new MethodNotAllowedException(); 
     } 
     if ($this->Test->delete($id)) { 
      $this->Session->setFlash('The post with id: ' . $id . ' has been deleted.'); 
      $this->redirect(array('action' => 'index')); 
     } 
    } 
} 

答えて

1

あなたの方法では、$this->Test->create();を含めることができませんでした。新しい行がテーブルに追加され、新しいデータが挿入されます。

public function add() { 
    if ($this->request->is('post')) { 
     $this->Test->create(); // missed this line 
     if ($this->Test->save($this->request->data)) { 
      $this->Session->setFlash('Your post has been saved.'); 
      $this->redirect(array('action' => 'index')); 
     } 
     else { 
      $this->Session->setFlash('Unable to add your post.'); 
     } 
    } 
} 
関連する問題