2012-11-09 3 views
0

이 두 요청을 1로 병합하고 싶습니다. 그러나이 작업을 수행하는 방법에 대한 단서가 없습니다. 어떤 생각? Doctrine : 1에서 2 요청 합치기

$productsCount = Doctrine::getTable('Product') 
      ->createQuery('p') 
      ->where('p.store_id = ?', $store_id) 
      ->andWhere('p.collection = ?', $this->product->getCollection()) 
      ->andWhere('p.image_path IS NOT NULL') 
      ->count(); 

$productsCollection = Doctrine::getTable('Product') 
      ->createQuery('p') 
      ->where('p.store_id = ?', $store_id) 
      ->andWhere('p.collection = ?', $this->product->getCollection()) 
      ->andWhere('p.status_id = ?', Product::_ONLINE) 
      ->andWhere('p.id<>?', $this->product_id) 
      ->offset(rand(0, $productsCount - 1)) 
      ->execute(); 
  • 교리 : 1.2
  • 심포니 : 1.4
  • PHP : 조회가 동일하지 않기 때문에 5.3
+0

: 나는 당신에게 교리 쿼리 최적화에 대한 흥미로운 프리젠 테이션을 추천? 이 2 개의 쿼리는 다른 정보를 반환합니다. 어떻게 병합 하시겠습니까? – j0k

+0

2 개의 쿼리는 동일한 테이블을 사용하고 첫 번째 테이블은 두 번째 쿼리에서 사용되므로이 작업을 최적화하기 위해이 모든 작업을 수행하는 것이 더 나을 것이라고 생각했습니다. 하지만 아마도 내가 틀렸어? – MaximeBernard

답변

1

당신은 하위 쿼리를 사용할 수 있습니다. 여기 DQL: Doctrine Query Language 몇 가지 예입니다. 그리고 여기 pseudo code가 있습니다. 그것이 즉시 작동하는지 모르겠습니다.

$q = Doctrine_Query::create() 
      ->from('Product p') 
      ->select('id, sum(id) as sumEntries') 
      ->addSelect('(SELECT id, name) // and else fields that you need 
         FROM Product a 
         WHERE (
         a.store_id = '.$store_id.' 
         AND 
         a.collection = '.$this->product->getCollection().' 
         AND 
         a.id<>= '.$this->product_id.' 
         ) 
         OFFSET '.rand(0, $productsCount - 1).') // I am not sure in this line 
         as resultSubquery') 

      ->where('p.store_id = ?', $store_id) 
      ->andWhere('p.collection = ?', $this->product->getCollection()) 
      ->andWhere('p.image_path IS NOT NULL') 


    $result = $q->execute(array(), Doctrine_Core::HYDRATE_ARRAY); //This greatly speeds up query 

배열은 $result입니다. var_dump()으로 가서 내용을 확인하십시오. 이 코드가 한 번에 작동하는지 확신 할 수 없지만이 방향으로 이동하는 것이 좋습니다.

p.s : 당신이 말하고자하는 일 Doctrine 1.2 Optimization

+0

아하이오! 그래서 당신은 Doctrine 질의 안에 순수한 SQL을 주입해야합니까? 나는 제품을 요청하고 동시에 세는 것이 더 최적화 될 것이라고 생각했다. – MaximeBernard

+1

이 작업을 수행하는 또 다른 방법은 잘 모르겠습니다.이 방법은 설명서에 설명되어 있으며 작동합니다. 두 가지 요청을 수행하는 것보다 낫다고 생각합니다 .- – denys281

+0

SQL 인젝션 공격에 노출되지 않았습니까? 이런 식의 쿼리? 쿼리는 매개 변수화되어야합니다. – oalbrecht