2

我想简单的问题。

我的控制器正在使用$this->uri->segment(3). 这将始终是一个值。我把它放在一个数组中传递给模型:

$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

4 回答 4

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

在你的模型。

于 2013-03-03T10:51:39.697 回答
2

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

前任:

get_customer_records_to_delete($customerid)
{
    // now $customerid is accessible
    $query = ....;
    return $……;
}
于 2013-03-03T10:55:03.690 回答
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
}
于 2013-03-03T11:01:50.800 回答
0

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

于 2015-06-19T19:56:04.327 回答