我使用Laravel V3,有口才很好,MySQL数据库为这个应用程序Laravel雄辩ORM许多一对多搜索查询
我有一个雇员数据库,以显示员工列表的搜索表单符合搜索条件。您可以填写一个或多个字段,并且仅匹配所有填写的字段。如下图所示: http://i.stack.imgur.com/Ttfhr.png
正确搜索所有功能,除了我刚添加了“训练”的多选。员工可以有零至多个附属于他们的培训证书。如果用户要搜索名字“Matt”,并在培训搜索条件下勾选“CSTS”,则该列表将显示所有名字与Matt相似的员工,并且还具有CSTS培训。
数据库模式如下:
user
--id
--first_name
--last_name
--job_title_id
etc...
training
--id
--name
--issused_date
--expiry_date
user_training
--id
--user_id
--training_id
模型的用户和培训如下:
class User extends Eloquent {
public static $table = 'user';
public function training(){
return $this->has_many_and_belongs_to('training','user_training');
}
class Training extends Eloquent {
public static $table = 'training';
public function users(){
return $this->has_many_and_belongs_to('user','user_training');
}
}
我有以下的代码,该函数搜索,什么我尝试了培训:
Route::post('(:bundle)/list', function() {
$search = new StdClass();
$search->first_name = Input::get('first_name');
$search->last_name = Input::get('last_name');
$search->job_titles = Input::get('job_titles'); // array of ids/returns null if none selected
$search->training = Input::get('training'); // array of ids/returns null if none selected
$search->city = Input::get('city');
$search->province = Input::get('province');
$search->phone = Input::get('phone');
$search->status = Input::get('status');
$search->gender = Input::get('gender');
$search->hire_from_date = Input::get('hire_from_date');
$search->hire_to_date = Input::get('hire_to_date');
$search->current_location = Input::get('current_location');
$search->role = Input::get('role');
Session::put('search', $search); // so we can access inside query builder sub-functions
$query = User::where('active', '=', true);
if(!empty($search->training)) { // one or many
$query = User::with(array('training' => function($query) {
$s_search = Session::get('search');
//$query->where_in('training.id', $s_search->training);
//foreach($s_search->training as $id) {
// $query->where('training.id', '=', $id);
//}
}));
}
//$query = User::join('user_training', 'user.id', '=', 'user_training.user_id');
//$query->join('user_training', 'user.id', '=', 'user_training.user_id');
//$query->join('training', 'training.id', '=', 'user_training.training_id');
if(!empty($search->first_name)) { $query->where('first_name', 'LIKE', '%' . $search->first_name . '%'); }
if(!empty($search->last_name)) { $query->where('last_name', 'LIKE', '%' . $search->last_name . '%'); }
if(!empty($search->city)) { $query->where('city', 'LIKE', '%' . $search->city . '%'); }
if(!empty($search->province)) { $query->where('province_id', '=', $search->province); }
if(!empty($search->gender)) { $query->where('gender', '=', $search->gender); }
if(!empty($search->phone)) { $query->where('phone_1', 'LIKE', '%' . $search->phone . '%'); }
if(!empty($search->status)) { $query->where('status_id', '=', $search->status); }
if(!empty($search->role)) { $query->where('role_id', '=', $search->role); }
if(!empty($search->current_location)) { $query->where('location_id', '=', $search->current_location); }
if(!empty($search->hire_from_date)) // "after"
$query->where('hire_date', '>=', date('Y-m-d', strtotime($search->hire_from_date)));
if(!empty($search->hire_to_date)) // "before"
$query->where('hire_date', '<=', date('Y-m-d', strtotime($search->hire_to_date)));
if(!empty($search->job_titles)) { // one or many
$query->where(function($query){
$s_search = Session::get('search');
foreach($s_search->job_titles as $id) {
$query->or_where('job_title_id', '=', $id);
}
});
}
$query->order_by('last_name');
$user_list = $query->distinct()->get();
$user_count = $query->count();
var_dump($user_list); die;
if(Input::get('action') == 'export') {
if(Permission::check("Export Users CSV")) {
$now = new DateTime();
return Response::download(User::get_group_csv($user_list), date_format(new DateTime(), 'Y-m-d') . '_User_Export.csv');
}
}
的“$用户>培训”成为作为ID的简单排列,其中M使用Training-> id表
作业标题搜索功能与multiselect正确配合,但job_title_id位于User表中,而不是关系。
我试过循环,手动加入,使用渴望加载,使用雄辩的“与”,都没有成功。有人能指引我朝着正确的方向吗?
有很多信息在那里完成的话,“有人指点我在正确的方向?”。但我不明白你想达到什么目的,你想指出什么方向? – AdrianHHH
最终目标是能够在多选中选择任意数量的“培训”项目,当您单击搜索时,它将只返回具有培训证书的用户 – Matt
@Antonio Carlos Ribeiro感谢您的帮助 – Matt