如何在zf2中的控制器中执行查询

How can I execute query in controller in zf2. Please help me

My query like this:

    $fetchGeography = "select ggl.list_id
                                from gst_geography_list as ggl
                                join gst_position as gp on(ggl.list_id = gp.list_id)
                                join gst_employee_geography_relation as gegr on(gp.position_id = gegr.list_id)
                                where gegr.employee_id = '" . $id . "' ";
    //var_dump($fetchGeography);die;


    $fetchGeographyList = MainDbTable::selectProcStatic($fetchGeography);

In ZF2, first you need the db adapter available in the MainDbTable class -> selectProcStatic static method.

Then do this -

public static function selectProcStatic($sql = '') {
    $resultSet = null;

    if($sql !== '') { 
        $statement = $this->adapter->query($sql); 
        $resultSet = $statement->execute();
    }

    return $resultSet;
}

If you have TableGateway available in the class then do this -

public static function selectProcStatic($sql = '') {
    $resultSet = null;

    if($sql !== '') { 
        $statement = $this->tableGateway->adapter->query($sql); 
        $resultSet = $statement->execute();
    }

    return $resultSet;
}

In any case, the db adapter is must.