2012-07-03 2 views
0

Zend 프레임 워크에 구축중인 애플리케이션의 "판매자"테이블에서 데이터를 검색해야하는 고유 한 조건과 명령 집합이 있습니다.복잡한 Zend 동일한 사용자 테이블의 쿼리

클라이언트는 기본적으로 디렉토리 페이지가 매우 특정 순서로 판매자를 나열하는 응용 프로그램을 요청 :

    지난 7 일 승인 된
  1. 판매 (다음 순서 아래 # 4에 의해)
  2. 그런 다음 사이트에서 업그레이드 된 기능에 대해 비용을 지불하고 7 일 이상 된 판매자 (아래 4 번 주문)
  3. 그런 다음 7 일 이상 7 일 이상 된 판매자 이전 (다음 # 4 주문)
  4. 위의 모든 경우 초 일일 주문은 출시 날짜, 기업 이름은 알파

위의 올바른 순서로 데이터를 반환하는 액션 도우미를 작성하는 가장 효과적인 방법을 알아 내려고하고 있습니다. 내보기의 경우에만 1,2 (및 4)가 필요하지만 응용 프로그램 내의 다른보기는 모두 4가 필요합니다.

지금 당장은 두 개 또는 세 개의 별도 쿼리를 작성하여 2 또는 3으로 전달했습니다. 뷰 내부에 있지만 제대로 작성된 코드를 찾기 위해 노력하고 있으며 3 개의 쿼리를 하나의 부분 루프에 전달할 수있는 하나의 객체로 결합하거나 하나의 쿼리를 작성하려고합니다. 어떻게 할 수 있습니까?

class Plugin_Controller_Action_Helper_ListSellers extends Zend_Controller_Action_Helper_Abstract 
{ 

//put your code here 
public function direct($regulars = false, $filter = false) 
{ 
    $dateMod = $this->dateMod = new DateTime(); 
    $dateMod->modify('-7 days'); 
    $formattedDate = $dateMod->format('Y-m-d H:i:s'); 
    // get sellers initialized in last 7 days 
    $sellerTable = new Application_Model_DbTable_Seller(); 



     // get sellers initialized in last 7 days 
     $select = $sellerTable->select()->setIntegrityCheck(false); 
     $select->from(array('b' => 'seller'),array('sellerID', 'businessName','sellerPicture')); 
     // select firstName, lastName, picture from user table, and businessName and sellerID from seller table. All records from seller table 
     $select->join(array('u' => 'user'), 's.userID = u.userID', array('firstName', 'lastName')); 
     $select->order('s.launchDate DESC','s.businessName ASC'); 
     $select->where('s.active = 1 AND s.contentApproval = 1 AND s.paymentApproval = 1'); 
     $select->where('s.launchDate > ?', $formattedDate); 
     if($filter){ $select->where('s.categoryID = ?', $filter);} 
     $newSellers = $sellerTable->fetchAll($select); 



     $query = $sellerTable->select()->setIntegrityCheck(false); 
     $query->from(array('b' => 'seller'),array('sellerID', 'businessName','sellerPicture')); 
     // select firstName, lastName, picture from user table, and businessName and sellerID from seller table. All records from seller table 
     $query->join(array('u' => 'user'), 's.userID = u.userID', array('firstName', 'lastName')); 
     $query->order('s.launchDate DESC','s.businessName ASC'); 

     $query->where('s.active = 1 AND s.contentApproval = 1 AND s.paymentApproval = 1 AND s.featured = 1'); 
     $query->where('s.launchDate < ?', $formattedDate); 
     if($filter){ $select->where('s.categoryID = ?', $filter);} 
     $featuredSellers = $sellerTable->fetchAll($query); 


    if($regulars){ 
     $where = $sellerTable->select()->setIntegrityCheck(false); 
     $where->from(array('b' => 'seller'),array('sellerID', 'businessName','sellerPicture')); 
     // select firstName, lastName, picture from user table, and businessName and sellerID from seller table. All records from seller table 
     $where->join(array('u' => 'user'), 's.userID = u.userID', array('firstName', 'lastName')); 
     $where->order('s.launchDate DESC','s.businessName ASC'); 

     $where->where('s.active = 1 AND s.contentApproval = 1 AND s.paymentApproval = 1 AND s.featured IS NULL'); 
     $where->where('s.launchDate < ?', $formattedDate); 
     $regularSellers = $sellerTable->fetchAll($where); 
    } 

} 
} 

답변

2

내가 어떤 한계가 쿼리에 적용되는 표시되지 않습니다 :

는 다음 순간에 내 도우미입니다. 그렇다면 을 모두 선택하고 개의 일치하는 레코드를 선택 하시겠습니까? 확장 성 이유로 인해 필자는 대답이 '아니오'여야한다고 추측 할 수 있습니다. 적용되는 제한이있을 것입니다. 이 경우 3 가지 다른 쿼리를 수행하면됩니다.

그러나 적용 할 제한이 없으면 필터링되지 않고 정렬되지 않은 모든 판매자를 선택하고보기 도우미 또는보기에서 정렬 및 필터링을 수행하는 단일 쿼리를 수행 할 수 있습니다.

Zend가 내장 된 Model-View-Controller pattern을 사용하려는 경우 컨트롤러 레이어 내에 데이터베이스 쿼리를 넣지 않는 것이 좋습니다. 컨트롤러는 얇아야합니다. 모델은 모든 데이터베이스 쿼리를 처리해야하며 결과를 컨트롤러에 보내면됩니다. 나는 Data Mapper pattern을 광범위하게 사용한다. 같은 뭔가 : 당신의 fetchX() 방법

$mapper = new Application_Model_SellerMapper(); 

$newSellers = $mapper->fetchNewSellers(); 
$featuredSellers = $mapper->fetchFeaturedSellers(); 
$regularSellers = $mapper->fetchRegularSellers(); 

각각 Zend_Db_Table_Row 경우보다는, Application_Model_Seller 인스턴스의 배열을 반환합니다.

이렇게하면 유지 보수가 용이 한 코드로 Separation of ConcernsSingle Responsibility Principle을보다 잘 유지 관리 할 수 ​​있습니다. 비록 당신이 장기간 프로젝트의 유일한 개발자 일지라도, 지금으로부터 6 개월 후에 당신은 당신이 쓴 것과 그 이유를 기억하지 못할 것입니다. 그리고 다른 누군가가 프로젝트에 올 경우 명확성은 이되고 실제로는이됩니다.

+0

예,'Zend_Paginator'를 플러그인 할 계획이므로 모든 결과가 쿼리에서 선택됩니다. 시간이 좀 걸렸지 만 쿼리를 도우미에서 모델로 이동하면 무슨 뜻인지 알 수 있습니다. 그 결과는 뷰 헬퍼'partialloop'에 여전히 전달 될 수 있습니까? –

+0

예, 'partialloop'에 전달할 수 있습니다. 어떤 배열이나 심지어 public 속성을 가진 객체를 받아들입니다. 그러나 Zend_Paginator는 내가 기억 하듯'Zend_Db_Select' 문을 요구합니다. 그 쪽 매 개 변수를 사용하면 이런 식으로 MVC에 반대하지만 모든 OOP 규칙에는 예외가 있습니다. – curtisdf

+0

이 응용 프로그램은 잠재적으로 1,000의 판매자를가집니다. Zend_Paginator에 대한 대안에 대한 제안이 있다면,이 많은 코드를 둘러 보자. 사용자는 여전히 검색 외에도 제공 업체 목록을 통해 다음/이전 검색을 수행 할 수 있어야합니다. 가져 오기 프로그램의 매개 변수를 제한 하시겠습니까? –