2017-03-08 9 views
1

하지만, 매개 변수로 하나의 열을받을 수 있습니다 : 그래서웅변 쿼리에 테이블에 값 쌍에 대한 MySQL의 검색을 변환 내가 문제 <code>whereIn()</code>라고 웅변 쿼리 빌더의 방법이있다 웅변</p>을 <pre><code>SELECT * FROM foo WHERE (column1, column2) IN (('foo', 1), ('bar', 2)) </code></pre> <p>사용하여 MySQL의 쿼리 이런 종류의 재현을 데

/** 
* Add a "where in" clause to the query. 
* 
* @param string $column 
* @param mixed $values 
* @param string $boolean 
* @param bool $not 
* @return $this 
*/ 
public function whereIn($column, $values, $boolean = 'and', $not = false) 
{ 
    ... 
} 

,이

$qb = $this->model->whereIn(['column1', 'column2'], array([1, 2], [1,3], [3, 32])); 
과 같은 일을 할 수 없습니다

나는 현재 해결책을 찾기 위해 노력하고 매우 열심히 노력하고 있지만, 누군가가 도울 수 있다면, 나는 :) 매우 감사 할 것

편집 : 관리 는 이런 식으로해야 할 일 :

/** 
* ... 
*/ 
public function findConnectionsByUser(User $user, array $userConnectionIds) 
{ 
    $qb = $this->model->query(); 

    ... 

    return $this->createQueryBuilderForUserConnectionsWithUserIds($qb, $user, $userConnectionIds)->get(); 
} 

/** 
* @param Builder $qb 
* @param User $user 
* @param array $userConnectionIds 
* 
* @return Builder 
*/ 
private function createQueryBuilderForUserConnectionsWithUserIds(Builder $qb, User $user, array $userConnectionIds) 
{ 
    foreach ($userConnectionIds as $userConnectionId) { 
     $qb->orWhere(array(
      array('receiver_id', $user->id), 
      array('initiator_id', $userConnectionId) 
     )) 
      ->orWhere([ 
       ['receiver_id', $userConnectionId], 
       ['initiator_id', $user->id] 
      ]); 
    } 

    return $qb; 
} 

EDIT 2 (보다 확장 성 용액) : '|'

$qb = $this->model->query(); 
$oneSide = $this->model->newQuery()->where('receiver_id', '=', $user->id) 
      ->whereIn('initiator_id', $userConnectionsIds); 

return $qb->where('initiator_id', '=', $user->id) 
      ->whereIn('receiver_id', $userConnectionsIds) 
     ->union($oneSide)->get(); 

답변

2

당신은 그들 또한

$qb = $this->model->whereIn('column1', [1, 2, 3]) 
->orWhere(function ($query) { 
    $query->whereIn('column2', [2, 3, 32]); 
}); 

확실하지처럼 둥지를 시도 할 수 있습니다. 그냥 첫 번째 쿼리가 더 ORAND 쿼리와 같은 초처럼 읽고

$qb = $this->model->whereIn('column1', [1, 2, 3]) 
->whereIn('column2', [2, 3, 32]); 

첫번째 시도합니다.

+0

답변을보기 전에 문제를 해결할 수있었습니다. 그러나 작은 테스트를 위해 코드를 제 목적에 맞게 조정했는데 제대로 작동하는 것으로 보입니다. Tnx! –

0

그것은 약간 원유,하지만 당신은 같은 일부 그렇지 않으면 사용되지 않는 문자로 두 가지를 분리, 하나에 열을 연결할 수 있습니다. 에있어서이 같은 단순히으로 사용할 수있는 경우

+0

음 ... 나는 이것이 도움이 될 것이라고 생각하지 않습니다. –