Execution of mySQL query in jQuery.Post method

2019-07-25 10:20发布

I have created a javascript file that contains js to trigger an event onchange of a drop down list. The JS is below:

// /public/js/custom.js

jQuery(function($) {
    $("#parent").change(function(event){
        event.preventDefault();
        var parentID = $('#parent').val();
        var id = $('#id').val();
        $.post("/menu/GetMenuChildren", {pID: parentID, thisID: id }, 
            function(data){
                if(data.response === true){        
                    var $el = $("#Position");
                    $el.empty(); // remove old options
                    $.each(data.newOptions, function(key, value) {
                      $el.append($("<option></option>")
                         .attr("value", value).text(key));
                    });
                } else {
                    // print error message
                    alert("something went wrong in Post");
                }
            }, 'json');

        alert("After Post");
    });
});

In my Controller.php I have an function GetMenuChildrenAction as shown below:

public function GetMenuChildrenAction()
{
    $request = $this->getRequest();
    $response = $this->getResponse();
    if ($request->isPost()) 
        {
        $post_data = $request->getPost();
        $parent_id = $post_data['pID'];
        $id = $post_data['thisID'];
        //$this->form->get('Position')->SetOptionValues(
        $newOptions = $this->getMenuTable()->GetPositionsByID($parent_id, $id);
        if(isset($newOptions))
        {
            $response->setContent(\Zend\Json\Json::encode(array('response' => true, 'newOptions'      => $newOptions)));
        }
        else
        {
            $response->setContent(\Zend\Json\Json::encode(array('response' => false)));
        }
    }
    return $response;
}

In the MenuTable.php there is a Method GetPositionsByID as shown below:

public function GetPositionsByID($parentID, $id)
{
    if($parentID == 0)
    {
        $menu = $this->getMenu($this->id);
        $parentID = $menu->parent_id;
    }
    if(isset($parentID))
    {
        $sql = "Select ID,Label from Menu Where parent_ID = " . $parentID . " and id > 1 and id <> " . $id . " Order by Position,Label";

        try
        {
           $statement =  $this->adapter->query($sql);
        }
        catch(Exception $e) {
            console.log('Caught exception: ',  $e->getMessage(), "\n");
        }

        $res = $statement->execute();

        $rows = array();
        $i = 0;
        foreach ($res as $row) {
            $i = $i + 1;
            $rows[$i] = array (
                $i => $row['Label']
            );
        }

        return $rows;
    }
    return array();
}

It all seems to be hooked up correctly, when I debug the code, I get the this line:

$statement =  $this->adapter->query($sql);

and then nothing happens. If I replace all the code in the GetPositionsByID method, and simply return an array like the following:

return array('1' => 'one', '2' => 'two');

it works great, however i want to get the data from the DB. Does anyone know why the execute would fail on this line?

$statement =  $this->adapter->query($sql);

Thanks in advance

1条回答
贪生不怕死
2楼-- · 2019-07-25 11:01

The issue was that the adapter was null.

查看更多
登录 后发表回答