2013-06-21 95 views
0

我使用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表中,而不是關係。

我試過循環,手動加入,使用渴望加載,使用雄辯的「與」,都沒有成功。有人能指引我朝着正確的方向嗎?

+0

有很多信息在那裏完成的話,「有人指點我在正確的方向?」。但我不明白你想達到什麼目的,你想指出什麼方向? – AdrianHHH

+0

最終目標是能夠在多選中選擇任意數量的「培訓」項目,當您單擊搜索時,它將只返回具有培訓證書的用戶 – Matt

+0

@Antonio Carlos Ribeiro感謝您的幫助 – Matt

回答

1

首先,您不需要使用會話來將變量放入匿名函數中。嘗試使用「用」代替。

// $search already defined 

$query = User::where('active', '=', true); 

if(!empty($search->training)) { // one or many 
    $query = User::with(array('training' => function($query) uses ($search) { 
     // Now you can use $search in this context 
    })); 
} 

}

此外,您使用小寫字符串爲您的關係,但模型/類實際上是資本所以他們應該是這樣的:

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

第三,您聲明「$ query = User :: where('active','=',true);」然後覆蓋它。也許你正在尋找這個:

if (!empty($search->training)) { // one or many 
    $query = User::with(array('training' => function($query) uses ($search) { 
     // Now you can use $search in this context 
     $query->where_in('training.id', $search->training); 
    })); 
} else { 
    $query = User::with(array()); 
} 
$query = $query->where('active', '=', true); 

我可以給更多的見解,但我不是100%你的最終目標是什麼。

+0

感謝使用關鍵字的提示,這是有道理的,我從來沒有在laravel文檔中看到。 – Matt

+0

最終目標是能夠在多選中選擇任意數量的「培訓」項目,並且當您單擊搜索時,它將只返回具有這些培訓證書的用戶。 – Matt

+0

我可以在job_title中使用'uses'keyowrd嗎? Deosnt似乎在工作。它與'where'子功能類似的情況。 < - 語言:郎PHP - > 如果{//一個或多個 \t \t \t $查詢 - >在哪裏(函數($查詢)使用((空($搜索 - > job_titles)!) $搜索){ \t \t \t \t的foreach($搜索 - > job_titles爲$ ID){ \t \t \t \t \t $查詢 - > or_where( 'job_title_id', '=',$ ID); \t \t \t \t } \t \t \t}); \t \t} – Matt