2

I'm working on a symfony project (using Doctrine as well) and I'd like to implement a staggered search on one of the pages, per example:

User searches for an author in the first select2 box (which is pulling data from DB via Ajax), and once an item is selected, there is a second select2 box called title, which I would like to display only titles belonging to the selected author.

Here's the controller side code (both Ajax and controller) for the initial box. Any ideas how I could construct the query for the second select2?

The part that related to the initial select2 that queries the DB for the results and autosuggested items:

public function searchAjaxAuthorAction()
{
    $em = $this->getDoctrine()->getManager();

    $term = $this->get('request')->query->get('term');
    $limit = $this->get('request')->query->get('page_limit', 1);

    $rep = $em->getRepository('StephenPersianArtBundle:Main');

    if($term){
        $entities = $rep->createQueryBuilder('m')
            ->where('m.orig_author LIKE ?1')
            ->orderBy('m.orig_author', 'ASC')
            ->setParameter('1','%'.$term.'%')
            ->getQuery();
    }else{
        $entities = $rep->createQueryBuilder('m')
            ->groupBy('m.orig_author')
            ->getQuery();
    }

    $entities = $entities->execute();

    $resultset = array();

    foreach($entities as $entity){
        if($entity->getOrigAuthor()){
            $resultset[] = array(
                //'id' => $entity->getId(),
                'id' => $entity->getOrigAuthor(),
                'text' => $entity->getOrigAuthor()
            );
        }
    }

    $return = json_encode($resultset);//jscon encode the array
    return new Response($return,200,array('Content-Type'=>'application/json'));
}

There's also another part related to this that basically loads data into a table based on the item selected in the select2, but I don't consider this relevant for the issue I'm having as all this is happening before the final query is completed.

Any help will be much appreciated!

TuxMeister
  • 275
  • 1
  • 8
  • 20

0 Answers0