0

我正在使用 Laravel v3、Eloquent 和一个用于此应用程序的 MySQL 数据库

我有一个员工数据库,其中包含一个搜索表单,其中显示了与搜索条件匹配的员工列表。您可以填写一个或多个字段,它只会匹配所有填写的字段。如下所示: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 搜索功能与多选正确,但 job_title_id 在 User 表中,而不是关系。

我尝试过循环、手动加入、使用急切加载、使用 Eloquent 的“with”,但都没有成功。有人可以指出我正确的方向吗?

4

1 回答 1

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% 知道您的最终目标是什么。

于 2013-06-21T21:27:55.460 回答