2013-07-05 46 views
0

我从模型传递一个参数,以查看这样get_where()是否允许多个参数?

模型

class school_model extends CI_Model{ 

function employee_get($student_id){ 
$query = $this->db->get_where('students', array('student_id'=>$student_id)); 
return $query->row_array(); 
} 

} 

控制器

function bret($student_id){ 
    $this->load->model('school_model'); 
    $data = $this->school_model->employee_get($student_id); 
    echo $data['student_gender']; 
    } 

这显然转换为给定的例如和通过浏览器等http://example.com/env/at/index.php/frontpage/bret/306

看出 select * from students where id=id

我想知道get_where()是否合适如果我想有这个疑问

select student_gender,student_has_a_medical_condition from students where (student_gender = 'female' && student_has_a_medical_condition = 'no') LIMIT 40;

我将需要延长get_where()为它工作?

回答

1

首先,我建议您阅读ActiveRecord类的CodeIgniter的优秀文档。

你不必延长get_where()只是利用现有的方法来定义查询:

function employee_get($student_id){ 
$this->db->select('student_gender','student_has_a_medical_condition'); 
$query = $this->db->get_where('students', array('student_id'=>$student_id,'student_has_a_medical_condition'=>'no','student_gender'=>'female'),40); 
return $query->row_array(); 
} 

当然你也可以在其他参数传递给函数,使他们没有硬编码的,而你也可以作为参数传递到所需的列中。但从文档开始。

+0

我希望找到诸如'http:// example.com/env/at/index.php/frontpage/bret/female/no /'这样的网址,可以使用提取'uri'来完成,但只能使用参数吗?我希望你明白我在说什么。 –

+0

那么你可以在''frontpage''控制器'bret'方法中添加可选参数:'bret($ student_id,$ gender = null,$ condition = null)',然后在空测试后将这些参数传递给你的模型。或者你问你是否可以从模型中检测这些URI段并将其用作条件? – Wolf

+0

是的,这就是我问的问题。 –

相关问题