Laravel Eloquent ORM多对多搜索查询

时间:2013-06-21 20:49:42

标签: php mysql laravel eloquent laravel-3

我正在使用带有Eloquent的Laravel v3和用于此应用程序的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-&gt; training”以id的简单数组形式出现,与training-&gt; id表匹配

“职务名称”搜索功能与多选项一起正常运行,但是job_title_id位于“用户”表中,而不是关系。

我尝试使用Eloquent的“with”循环,手动加入,使用预先加载,但都没有成功。有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分: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%的最终目标。