We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Very strange behavior with model

(To know what's all about, please read this: https://forum.phalcon.io/discussion/3195/stored-procedure-with-from-as-a-subquery )

I've a problem know. I had two actions: each of these return an array, wich contains many arrays. Each one of these lasts arrays has two variables inside (string and integer).

When I use the first action (only does a var_dump() of the model's returned data), I can see that it returns data perfectly. However, with the second action, when I do a $response->setJsonContent(array(...)), I can only retrieve, in the $.ajax(), the integer that form part of the last array (I cannot retrieve the super array or the string variable)...

Has $response->setJsonContent(array(...)) any strange behavior that affects an array containing arrays?



98.9k

No, it does not have any strange behavior that affects an array containing arrays. It just calls json_encode: https://github.com/phalcon/cphalcon/blob/2.0.0/phalcon/http/response.zep#L441



33.8k
edited Aug '14

Yeah, I thought it too. But it's strange:

$response->setJsonContent(array('response' => ·$results)) // Undefined
$response->setJsonContent(array('response' => ·$results[0])) // Undefined
$response->setJsonContent(array('response' => ·$results[0]["string"])) // Undefined or null (I think null, don't remember now)
$response->setJsonContent(array('response' => ·$results[0]["id"])) // Returns and integer, and that id is correct in the data base. What?


33.8k

This is what I get when I do a var_dump($response):

object(Phalcon\Http\Response)#44 (6) {
  ["_sent":protected]=>
  bool(false)
  ["_content":protected]=>
  bool(false)
  ["_headers":protected]=>
  NULL
  ["_cookies":protected]=>
  NULL
  ["_file":protected]=>
  NULL
  ["_dependencyInjector":protected]=>
  NULL
}

So... _content is always false, only if I do a setJsonContent(array("answer" => $results)), no matter whatever type of variable is $results . But, if I just do a setJsonContent(array("answer" => "loading...")), _content is setted correctly. Why? (and doing a var_dump($results) I can see that I've found students with the stored procedure)

dblcheck $results ... maybe its object that cannot be json encoded or something like that ... thats only explanation :)



33.8k

$results it's ok, the problem is that when $results comes from the model, I can var_dump() his data, and it's OK. But, doing a json_encode(), it doesn't work. If I copy-paste the same string that has $results to make it a JSON, it works.

For any reason I just cannot understand (magic), I cannot return JSON content, whatever type of variable I return.

try using array instead of object with json_encode()



33.8k
edited Aug '14

What?

EDIT: I keep trying to find new answers, but there isn't anyone for this.

        $response = new Response();
        $resultados = Tabla::buscarAlumnos("3");

        echo "<pre>";
        var_dump($resultados);
        echo "<h1>-----------------------</h1>";
        foreach ($resultados as $resultado)
        {
            echo "<p>" . $resultado["id"] . "###" . $resultado["alumno"] . "</p>";
        }
        echo "<h1>-----------------------</h1>";
        $prueba = "";
        foreach ($resultados as $resultado)
        {
            $prueba .= ($resultado["id"] . "###" . $resultado["alumno"] . "*");
        }
        echo $prueba;
        echo "<h1>-----------------------</h1>";
        $response->setJsonContent(array("respuesta" => $prueba));
        // bool(false)
        var_dump($response->getContent());
        $response->setJsonContent(array("respuesta" => "5###3º ESO C - EE EEE, E*6###3º ESO C - FF FFF, F*"));
        // string(76) "{"respuesta":"5###3\u00ba ESO C - EE EEE, E*6###3\u00ba ESO C - FF FFF, F*"}". What?
        var_dump($response->getContent());


33.8k
Accepted
answer

Four days... four days with the problem. And what was the problem? The "º" symbol in 3º ESO C - EE EEE, E.

A symbol... just a symbol...