Recuperation des donnée sou format json

Par Mehdi Moalla, il y a 11 ans


Bonjour
je voudrais récuperer des donnée sous format json car j'utilise mongodb avec cakephp

j'ai mis ceci :

$patients = $this->Patient->find('all');

et voici le résultat :

array( (int) 0 => array( 'Patient' => array( 'reference' => 'p-203', 'nom' => 'Moalla', 'prenom' => 'Mehd', 'birthdate' => '1988-08-08', 'sexe' => 'M', 'nationalite' => 'tun', 'job' => 'etud', 'adresse' => 'jhkjhkjhkjh', 'ville' => 'tn', 'tel' => '565478465', 'email' => 'm@m.com', 'note' => 'hghg', 'age' => '20', 'numss' => '36461654', 'id' => '5489f9faec88a9202100002b' ) ) )

je veux changer le format d'affichage vers json.<br/>
Merci.

2 réponses

Inspirat, il y a 11 ans

Il faut modifier plusieurs fichiers pour arriver à ça:

// app/Config/routes.php Router::parseExtensions('json'); // app/Controller/PatientsController.php public $components = array('RequestHandler'); public function index() { $patients = $this->Patient->find('all'); // En suite, deux façon de récupérer les données json: // - Sans passer par un traitement de vue $this->set('_serialize', array('patients')); // - En passant par une vue $this->set(compact('posts', 'comments')); } // app/View/Patients/json/index.ctp (Si tu utilise la deuxième méthode dans ton action) <?= debug($patients); ?>

La documentation CakePHP (en français) au cas ou:

antho07, il y a 11 ans

Bonjour,

sinon un json_encode ne fonctionne pas ?