保存不正常

时间:2013-08-21 12:11:45

标签: php kohana

控制器

userdetails.php

class Controller_Userdetails extends Controller { 
    public function action_index() {
        $view = new View('userdetails/index');
        $this->response->body($view);
    }
     public function action_add() {//load adddetails.php
        $userdetails = new Model_Userdetails();         
        $view = new View('userdetails/adddetails');
        $view->set("userdetails", $userdetails);         
        $this->response->body($view);
    }
    public function action_post() {//save the post data
        $userdetails_id = $this->request->param('id');
        $userdetails = new Model_Userdetails($userdetails_id);
        $userdetails->values($_POST);       
        $userdetails->save();          
        $this->request->redirect('index.php/userdetails'); 
    }

}

视图

adddetails.php

<?php echo Form::open('userdetails/post/'.$userdetails->id); ?>
    <?php echo Form::label("first_name", "First Name"); ?>
    <?php echo Form::input("first_name", $userdetails->first_name); ?>
    <br />    
    <?php echo Form::label("last_name", "Last Name"); ?>
    <?php echo Form::input("last_name", $userdetails->last_name); ?>
    <br />
    <?php echo Form::label("email", "Email"); ?>
    <?php echo Form::input("email", $userdetails->email); ?>
    <br />
    <?php echo Form::submit("submit", "Submit"); ?>
<?php echo Form::close(); ?>

我正在尝试将数据插入数据库。名称和电子邮件字段正确加载,如果我输入值并点击输入它将重定向到其他页面,但对象未保存在数据库中。需要帮助解决此问题。

2 个答案:

答案 0 :(得分:2)

我有时会在使用

时发现保存问题
$userdetails->values($_POST);

尝试使用

$userdetails->first_name = $this->request->post('first_name');
$userdetails->last_name= $this->request->post('last_name');
$userdetails->email= $this->request->post('email');

答案 1 :(得分:1)

假设模型是ORM模型(class Model_Userdetails extends ORM {}),您应该使用ORM::factory()来加载模型。此外,建议使用expected参数以确保仅使用要插入的值。

$user_details = ORM::factory('Userdetails');
$user_details->values($this->request->post(), 
    array('first_name', 'last_name', 'email')
);
$user_details->save();