1

如何在控制器中使用模型的索引功能?

根据 codeigniter 用户指南,我们知道我们可以使用

$this->name_of_model->function_of_model();    

在控制器中。在重定向中,我们可以使用

redirect('controller_name/function_name');    

如果我们在控制器中使用索引函数,那么我们可以在重定向中使用

redirect('controller_name');

重定向到该控制器。这样我该如何使用

$this->name_of_model->function_of_model();

这种语法。

谢谢

4

2 回答 2

0

假设您在控制器索引中调用模型索引

Class test_controller extends CI_Controller
{
   function index(){

       $this->load->model('model_name');      
       $this->model_name->model_index();      

   }
}

现在,当您重定向到控制器索引时,模型索引将被自动调用。

编辑:但是,您可以像这样使用它

Class test_controller extends CI_Controller
{
   function index(){

       $query = $this->db->get('my_table');
       $data['query_result'] = $query->result();  

   }
}

型号索引是这个

function index()
{
  $query = $this->db->get('my_table');
  return $query->result();
}

您应该注意这里没有调用模型索引,因为没有它我们正在做的工作是没有用的模型。所以分离数据库交互是有用的,将它与控制器结合打破了 MVC 模式。

于 2012-11-01T05:29:59.810 回答
0
class User_model extends CI_Model
{
    function __construct()
    {

        parent::__construct();
        $this->load->database();
    }

    function insertrow($data,$table){

        $this->db->insert($table,$data);
    }

    function updaterow($data,$table,$id){
        $this->db->where($id);
        $this->db->update($table,$data);
    }

    function fetchrow($table){

        $query = $this->db->get($table);
        return $query->result();

     }

     function fetchrowedit($tbl_name,$id){

    $query = $this->db->get_where($tbl_name, $id); 

    return $query->result();
    }


    function fetchrowlogin($info,$table){

        $this->db->select('*');
        $this->db->where($info);
        $this->db->from($table);
        $query = $this->db->get();
        if($query->num_rows() > 0){
        $row = $query->row_array();
            return $row;
        }
    }


        function alreadyexits($table,$var){
        $query = $this->db
        ->select('*')
        ->where($var)
        ->get($table)
        ->num_rows();
        return $query;
        }

        function isVarExists($table,$var){
        $query = $this->db
        ->select('*')
        ->where($var)
        ->get($table)
        ->num_rows();
        return $query;
            }

    function messages_list($ids){

        $this->db->where('reciver_id', $ids);
        $this->db->limit(10);
        $this->db->order_by("time", "asc");
        $this->db->from('message_table');
        $query = $this->db->get(); 
        return $query->result();
    }
}
于 2018-05-03T05:48:38.370 回答