2011-08-03 4 views
5

여러 매개 변수를 사용하여 특정 모델을 필터링 할 수있는 Symfony2의 오픈 소스 (또는 예제) 코드가 있습니까? 이 Trulia 웹 페이지에서 제가 찾고있는 좋은 예가 보입니다.symfony2로 필터링

http://www.trulia.com/for_sale/30000-1000000_price/10001_zip/

http://www.trulia.com/for_rent/Chicago,IL/#for_rent/Chicago,IL/0-500_price/wd,dw_amenities/sm_dogs_pets"

http://www.trulia.com/for_rent/Chicago,IL/#for_rent/Chicago,IL/400-500_price/wd,dw_amenities http://www.trulia.com/for_rent/Chicago,IL/#for_rent/Chicago,IL/wd,dw_amenities"

http://www.trulia.com/for_rent/Chicago,IL/#for_rent/Chicago,IL/400p_price/dw,cs_amenities

http://www.trulia.com/for_rent/Chicago,IL/#for_rent/Chicago,IL/1p_beds/1p_baths/400p_price/dw,cs_amenities

양식을 클릭하면 URL이 어떻게 생성되는지 알 수 있습니다.이 모든 경로에 대해 하나의 컨트롤러를 사용하고 있습니다. 어떻게 끝났습니까? I 가능한 모든 경로를 특정 컨트롤러로 리다이렉트 할 것 같지 않습니다 (아래 참조). 일종의 동적 라우팅일까요?

/** 
* @Route("/for_rent/{state}/{beds}_beds/{bath}_bath/{mix_price}-{max_price}_price /{amenities_list} 
* @Route("/for_rent/{state}/{mix_price}-{max_price}_price/{amenities_list} 
* @Route("/for_rent/{state}/{bath}_bath/{mix_price}-{max_price}_price/{amenities_list} 
* @Route("/for_rent/{state}/{mix_price}_price/{amenities_list} 
* @Route("/for_rent/{state}/{beds}_beds/{bath}_bath/{amenities_list}  
* ........ 
*/ 

public function filterAction($state, $beds, $bath, $min_price, $max_price ....) 
{ 
    .... 
} 

감사합니다.

답변

1

간단한 쿼리 (예 : min-max 값과 같은 데이터 범위가 필요하지 않은 항목)의 경우 엔티티 저장소를 사용하여 주어진 요청 매개 변수별로 항목을 찾을 수 있습니다. 당신의 엔티티가 Acme\FooBundle\Entity\Bar이라고 가정 :

$em = $this->getDoctrine()->getEntityManager(); 
$repo = $em->getRepository('AcmeFooBundle:Bar'); 

$criteria = array(
    'state' => $state, 
    'beds' => $beds, 
    // and so on... 
); 
$data = $repo->findBy($criteria); 

$criteria 배열을 구축, 당신은 아마 어떤 논리를 원할 것이다 있도록 단지 종류가 제공 한 기준 대신에 가능한 모든 값에 의해. $data에는 기준과 일치하는 모든 엔티티가 포함됩니다.

더 복잡한 쿼리의 경우 추출하려는 엔티티를보다 세밀하게 제어하려면 DQL (및 아마도 custom repository)을 조사해야합니다.

1

경로를 구성하려면 설명서의 Routing 페이지를 살펴보아야하지만 경로에 요구 사항을 넣을 수 있는지 확인 했습니까? This 페이지에서 특수 효과를 사용하는 방법에 대해 설명합니다.

필터링에 관해서는 DQL이 괜찮을 것이라고 생각하지만 Doctrine을 사용하여 SQL을 작성하고 쿼리 결과를 하나 이상의 엔터티에 매핑 할 수도 있습니다. 이 내용은 here입니다. DQL보다 유연 할 수 있습니다.

+0

주석 당신은 위대 하나 개 이상의 경로를 일치시킬 수 있습니다,하지만 당신은 모든 필터 매개 변수를 수동으로 가능한 모든 조합을 쓰고 피하지 않는 내 말은, 가능한 모든 경로의 조합이 될 것입니다 ABC 매개 변수를 가지고, ABC, AB, BC, AC, 고통입니다. 나 자신의 라우팅 구현이 동적 URL 일치의 종류를 허용할지 여부를 모르겠다. – csg

+1

매우 간단한 해결책을 찾았습니다/** * @Route ("/ search/{q}", 요구 사항 = { "q": ". +"}) * ........ */ q는 URL의 끝까지 모든 것을 일치시킵니다. 그런 다음 "q"를 구문 분석하면됩니다. 자세한 정보는 http://symfony.com/doc/current/cookbook/routing/slash_in_parameter.html을 참조하십시오. – csg

0

csg의 경우, "일방적"방식 만 사용해야하는 경우 해결책이 좋습니다 (@Route ("/ search/{q})와 함께 사용하는 것이 좋습니다. 그러나 일부 가격 필터 링크를 인쇄해야하는 경우 URL 액세스 페이지 : @Route("/search/{q}의 경우 http://www.trulia.com/for_sale/30000-1000000_price/10001_zip/

당신이 경로 방법 URL이 PARAMS으로 생성 사용할 수 없습니다

0

당신이 후 복잡한 DQL을 생성하는 데 도움이 LexikFormFilterBundle "lexik/form-filter-bundle": "~2.0"라는 큰 번들이 있습니다. 사용자가 완료 한 필터 양식

Ic 주어진 FormType (SencioGeneratorBundle에 의해 생성 된 것과 같은)의 유형을 변경하는 Bundle을 redefined했습니다. 따라서 올바른 FilterForm을 표시 한 다음 그 뒤에 LQL을 사용하여 DQL을 만들 수 있습니다.

귀하는이하는 모든 각 엔티티 필드에 필요한 FormType을 제안 교리 종류를 추측 해를 우선이다이 README.md

다음, 작곡가로를 설치하고 적절한 LexikFormFilterType에 의해 주어진 유형을 대체 할 수 있습니다. 예를 들어, NumberTypefilter_number으로 바꾸면 최대 및 최소 간격 경계 두 개를 렌더링합니다.

private function createFilterForm($formType) 
{ 
    $adapter = $this->get('dd_form.form_adapter'); 
    $form = $adapter->adaptForm(
     $formType, 
     $this->generateUrl('document_search'), 
     array('fieldToRemove1', 'fieldToRemove2') 
    ); 
    return $form; 
} 

양식 제출시 내 예제와 같이 생성 된 쿼리를 Lexik에 제공하고 생성 된 쿼리를 실행하면됩니다. ,

public function searchAction(Request $request) 
{ 
    // $docType = new FormType/FQCN() could do too. 
    $docType = 'FormType/FQCN'; 
    $filterForm = $this->createFilterForm($docType); 
    $filterForm->handleRequest($request); 

    $filterBuilder = $this->getDocRepo($docType) 
     ->createQueryBuilder('e'); 
    $this->get('lexik_form_filter.query_builder_updater') 
     ->addFilterConditions($filterForm, $filterBuilder); 

    $entities = $filterBuilder->getQuery()->execute(); 

    return array(
     'entities' => $entities, 
     'filterForm' => $filterForm->createView(), 
    ); 
}