1

控制器

用户详细信息.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'); 
    }

}

意见

添加详细信息.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(); ?>

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

4

2 回答 2

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');
于 2013-08-29T10:34:21.643 回答
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();
于 2013-08-29T10:39:10.293 回答