codeigniter将变量从控制器传递给模型

时间:2013-03-03 10:46:16

标签: codeigniter activerecord model controller

我认为的简单问题。

我的控制器正在使用$this->uri->segment(3)从网址显示if。这将始终是单个值。我将它放在一个数组中以传递给模型:

$customerid = array(
   'id' => $this->uri->segment(3)
);

控制器语法如下:

function confirm_delete_customer()
{
            $data['title']="Confirm Customer Deletion";

            $customerid=array(
                'id'=>$this->uri->segment(3)
                );

            //query model to get data results for form
            $data=array();

            if($query=$this->model_master_data->get_customer_records_to_delete()){
                $data['records']=$query;


            $this->load->view("master_data/view_master_data_header",$data);
            $this->load->view("master_data/view_master_data_nav");
            $this->load->view("master_data/view_content_master_data_confirm_customer_deletion",$data);
            $this->load->view("master_data/view_master_data_footer");



}

然后我尝试访问此数组值并将其传递给我的模型进行处理。如果我将数组硬编码到模型中,它的工作方式如下:

模型 - 手动语法是:

function get_customer_records_to_delete()
{
    $query = $this->db->get_where('customers', array('id'=>43));
    return $query->result();
}

如果我尝试用我的控制器中的数组替换它,则会失败并显示错误: Undefined variable: customerid

我想要工作的模型的想法:

function get_customer_records_to_delete()
{
    $query = $this->db->get_where('customers', $customerid);
    return $query->result();
}

我觉得它很小。但这是从数据库中获取单个记录以输出到视图的最佳方法吗?

提前感谢您的帮助。

4 个答案:

答案 0 :(得分:4)

最好的方法是:

function confirm_delete_customer()
{
    $data=array();

    $data['title']="Confirm Customer Deletion";

    $customerId = $this->uri->segment(3);

    //Prevent SQL injections
    if(!is_numeric($customerId) || empty($customerId)) {
        show_error("Bad Request");
    }

    $query = $this->model_master_data->get_customer_records_to_delete($customerId);

    if ($query){
        $data['records']=$query;


        $this->load->view("master_data/view_master_data_header",$data);
        $this->load->view("master_data/view_master_data_nav");
        $this->load->view("master_data/view_content_master_data_confirm_customer_deletion",$data);
        $this->load->view("master_data/view_master_data_footer");

    }
}

然后你可以简单地打电话:

function get_customer_records_to_delete($customerId)
{
    $query = $this->db->get_where('customers', array('id'=>$customerId));
    return $query->result();
}

在你的模特身上。

答案 1 :(得分:2)

您需要将值作为参数传递给函数,以便它可以访问它。

例如:

get_customer_records_to_delete($customerid)
{
    // now $customerid is accessible
    $query = ....;
    return $……;
}

答案 2 :(得分:2)

你应该严重依赖功能参数。从控制器获取客户ID并将其发送到模型。此外,您可以使用row()从数据库中获取单个结果。

<强>控制器

    function confirm_delete_customer(){
    $data['title']="Confirm Customer Deletion";

    $customerid=$this->uri->segment(3);


    //query model to get data results for form
    $data=array();

    if($query=$this->model_master_data->get_customer_records_to_delete( $customerid)) //you are sending customer id as a parameter here
    $data['records']=$query;


    $this->load->view("master_data/view_master_data_header",$data);
    $this->load->view("master_data/view_master_data_nav");
    $this->load->view("master_data/view_content_master_data_confirm_customer_deletion",$data);
    $this->load->view("master_data/view_master_data_footer");



}}

<强>模型

function get_customer_records_to_delete($customerid) 
{
    $query = $this->db->get_where('customers', array("id"=>$customerid)); //you are using the customer id sent from the controller here
    return $query->row(); //this will return a single row
}

答案 3 :(得分:0)

旧线程,但答案是在控制器中将变量声明为“public”(即public $customerid;),在这种情况下,它将可供您的模型使用。在某些情况下,显式传递作为参数可能更安全。但是,当你有几个变量时,可以选择声明它们。

相关问题