Search code examples
phpmysqlphalcon

How to retrieve data from database Phalcon php


I'm using tutorial-master https://docs.phalconphp.com/en/latest/reference/tutorial.html the Create step from CRUD is awesome, I can input data to database. But I don't understand how to generating data from table using query.

this code didn't work because I use $application = new Application($di); not micro.

// Retrieves all robots $app->get('/api/robots', function () use ($app) {

$phql = "SELECT * FROM Robots ORDER BY name";
$robots = $app->modelsManager->executeQuery($phql);

$data = array();
foreach ($robots as $robot) {
    $data[] = array(
        'id'   => $robot->id,
        'name' => $robot->name
    );
}

echo json_encode($data);

});

I want to have $query="SELECT * FROM ospos ORDER BY ospoId"; and output $data = array(); echo jsone_encode($data) and resulting same result as micro code.. please help Thank you.


Solution

  • UPDATE: your above query using models.

    $robots = Robots::find([
        'order' => 'name'
    ]);