Search code examples
phparraysextjsextjs4extjs4.2

Passing an array via extraParams to a store only gives the last value in ExtJS


I am building a web application and there is a part there where I need to load records from a store given a set of ids. The way I went about this was to get all the ids, store them in an array, and then load the store with that array as an extra param.

Here is my code:

for(var i = 0 ; i < store.count() ; i++){
    console.log("id person = " + store.getAt(i).get("ID_PERSON"));
    idArray.push(store.getAt(i).get("ID_PERSON"));
}

console.log("id array = " + idArray);

store = Ext.getStore('borrowerListStore');

store.getProxy().extraParams={
    idArray: idArray
};

store.load({
    callback: function(records, operation, success) {

        var total = operation.request.scope.reader.jsonData['total'];
        var message = operation.request.scope.reader.jsonData['message'];

        console.log("message  = " + message);
    }

});

First, I go though my initial store then get all the ids and store them in an Array. After that, I declare my store, I get the proxy, I set the params, and then I use the idArray I assembled as the array.

The console line shows me proper values for example 77, 24, 80 so I know that I passed 3 values.

Then in my PHP code linked in the read method of my store, I do this:

else if(isset($_GET['idArray'])){

    $idArray = $_GET['idArray'];

    $array = $idArray;

    for($i = 0 ; $i < count($idArray) ; $i++){
        array_push($array, $idArray[$i]);
    }

    $sql = "SELECT * FROM TABLE WHERE ID IN(".implode(',',$array).")";

$result = mysql_query($sql);    

    $res->message .= " Loaded data ";
    $res->message .= " sql = " . $sql;
    $res->message .= " idArray = " . $idArray;
    $res->message .= " array = " . $array;

    $total = mysql_fetch_array($totalquery);
}

So that when the store loads and I enter the callback function, I can see the messages that got returned.

However, when I console out the messages, it seems to me that only the last array element is the only thing that my PHP received because the log would look like:

message  =  Message start  Loaded data  sql = SELECT * FROM TABLE WHERE ID IN() idArray = 80 array = 80

What's happening here? Why can't I seem to pass an array as extra param?


Solution

  • First of all, I am not sure whether you really want to transmit arrays as GET parameters. If the arrays become bigger, you run into max URL length issues. I would definitely recommend to use POST and transmit JSON to the server.

    ExtJS 4.2.2 sends the array to the server correctly, I have checked in a fiddle that this cannot be the issue.

    But I am not sure whether PHP understands the format in which it is sent. It seems as if when I call test.php?x=1&x=2, $_GET['x'] is not an array [1,2], but only a number 2. I am not sure why, though, I have not looked into PHP code. As a quick hack you could use

    store.getProxy().extraParams={
        idArray: idArray.join(',')
    };
    

    on the client side to transmit the ids as a string instead of array, and decode that string back into ids with

    $array = array_map("intval",explode(",",$_GET['idArray']));
    

    on the server side. (If you don't use intval and use the string without any check in the SQL query, you are prone to SQL injection attacks.)