ZF2 Ajax 调用 returns 所有信息而不仅仅是发送的 id

ZF2 Ajax call returns all information instead of just the id being sent

我正在尝试从下拉列表中 select 一个合同,但看起来 Ajax 调用正在返回所有合同的信息,而不仅仅是发送的 ID。请记住,我是 ZF2 的新手。

// view.phtml

<script>
function loadContractId(id)
 {
    $.getJSON('<?php echo $this->basePath();?>/ajax/getId/'+id+'', function(data) {


    $("#ctrid").text(data["arr"][0].ctr_id);
    $("#ctrspec").text(data["arr"].ctr_spec);
    $("#ctrnutype").text(data["arr"].ctr_nu_type);
    $("#ctrlocationcity").text(data["arr"].ctr_location_c);
    $("#ctrlocationstate").text(data["arr"].ctr_location_s);

   $.each(data["arr"], function (index, item) {
      console.log(item);
   });

    });
    $("#contact_holder").css('visibility','visible');
 }

</script>


<div id="loc_placement_panel" class="p0pup">
  <form name="loc_placement_form" method="post" action="<?php echo $this->basePath(); ?>/booking/view/add">
      <input type="hidden" name="ph_id" value="<?php echo $ar["ph_id"]; ?>">
      <input type="hidden" name="pres_status" value="2">
      <input type="hidden" name="ph_name" value="<?php echo $ar["ctct_name"]; ?>">

      <!--<input type="hidden" name="addon" value="see below">-->
      <strong>Placements</strong><br/><br/>

      <label><strong>Contract:</strong></label>

      <select name="contactlist" id="contactlist" onchange="loadContractId($('#contactlist').val())">
        <?php
                foreach ($ctrLT as $row=>$ar_contracts)
                {
                    echo "<option value='".$ar_contracts['ctr_id']."'>";
                    echo $ar_contracts['ctr_no'];
                    echo "</option>";
                }
            ?>
        </select>

        <div id="contact_holder" style="visibility: hidden">
            <strong>Ctr id: </strong><span id="ctrid" ></span><br/>
            <strong>Spec: </strong><span id="ctrspec" ></span><br/>
            <strong>Nurse Type: </strong><span id="ctrnutype" ></span><br/>
            <strong>City: </strong><span id="ctrlocationcity" ></span><br/>
            <strong>State: </strong><span id="ctrlocationstate" ></span><br/>
        </div>


      <label><strong>User Name:</strong></label>
      <input type="text" name="loc_location" id="loc_location" value="<?php echo $ar["ctct_name"]; ?>"  />


      <label><strong>$ltcontracts:</strong></label>
      <textarea id="txtArea" rows="10" cols="100" name="loc_location" id="loc_location" value=""><?php '<pre>'; print_r($ltcontracts); '</pre>';?></textarea>


      <br/><br/>
      <!-- <input type="submit" value="Submit" id="loc_placement_submit_btn" name="loc_placement_submit_btn" /> -->
      <input type="button" value="Cancel" id="loc_placement_cancel_btn" />
  </form>
</div>

// AjaxController.php

 // LT contracts
    public function getId($id) {
        $id = (int) $id;
        return $this->getResortObject('retainedResort',$id);
    }

  // LT contracts
    public function getIdAction() {

        $result = new \stdClass();

        $arr = $this->getContractsTable()->selectLtContracts($id);
        $result->code = Response::STATUS_CODE_200;

        $result->arr = $arr;
        $json = Json::encode($result);
        $response = $this->getResponse(); //new Response();
        $response->setStatusCode($result->code);
        $response->getHeaders()->addHeaders(array('Content-Type'=>'application/json'));
        $response->setContent($json);
        return $response;
    }

// ContractTable.php

我也尝试使用 selected id,( $select->where('ctr_id = ?', $id); ) 但它没有用。

public function selectLtContracts($id = 0, array $ar = null) {


    $this->table='allcontracts';
    $select = new Select($this->table);
    $select->where->like('ctr_no', '%LT');
    $resultSet = $this->selectWith($select);

    $ar = array();
    if($resultSet)
    {
    $i=0;
      foreach ($resultSet as $row) {

        $ar[$i]['ctr_id']=$row->ctr_id;
                $ar[$i]['ctr_no']=$row->ctr_no;
                $ar[$i]['ctr_spec']=$row->ctr_spec;
                $ar[$i]['ctr_nu_type']=$row->ctr_nu_type;
                $ar[$i]['ctr_location_c']=$row->ctr_location_c;
                $ar[$i]['ctr_location_s']=$row->ctr_location_s;
                $ar[$i]['ctr_nurse']=$row->ctr_nurse;
                $ar[$i]['ctr_type']=$row->ctr_type;
                $ar[$i]['ctr_marketer']=$row->ctr_marketer;
                $ar[$i]['ctr_recruiter']=$row->ctr_recruiter;
        $i+=1;
      }
    }

    return $ar;
  }

这是当我从下拉列表中 select 一个合同时从我的控制台得到的:

有什么想法吗?

基本上我忘了在 getIdAction() 中包含来自路由的单个 'id' 参数:

$id = (int) $this->params()->fromRoute('id', 0);

并且在 ContractsTable.php 中我需要添加 equalTo() predicate:

...

if($id!='' && $id > 0)
  $where->equalTo('ctr_id', $id);

  $select->where($where);

...