保存工作不正常


save not working properlly

控制器

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(); ?>

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

我有时会在使用时发现保存问题

$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');

假设模型是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();