Laravel v3、Eloquent、およびこのアプリケーション用の MySQL データベースを使用しています
検索条件に一致する従業員のリストを表示する検索フォームを備えた従業員データベースがあります。1 つまたは複数のフィールドに入力できます。入力されたすべてのフィールドでのみ一致します。以下に示すように: 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');
}
}
「$user->training」は ID の単純な配列として提供され、Training->id テーブルと一致します。
役職検索は複数選択で正しく機能しますが、job_title_id はリレーションシップではなくユーザー テーブルにあります。
Eloquentの「with」を使用して、ループ、手動での参加、熱心なロードの使用を試みましたが、すべて成功しませんでした。誰かが私を正しい方向に向けることができますか?