简体   繁体   中英

How to translate this code from PHP in AdonisJS?

I have some code in PHP and i should translate it in AdonisJS.But i don't know PHP.

This is for an endpoint for an api.

    $comenzi = $app['db']->fetchAll("select * from user_comenzi where user_id = ? order by id desc",array($user['id']));
    $comenziArray = array();
    foreach ($comenzi as $comanda) {
      $comandaObject = json_decode($comanda['comanda'],true);
      if (count($comandaObject['items']) === 0 ) {
        continue;
      }
      $comandaObject['id'] = $comanda['id'];
      $comenziArray[] = $comandaObject;
    }
    return $app->json($comenziArray);
  });

You guys don't need to translate it, you can just explain me what this code makes.

The database query finds all rows in the user_comenzi table where user_id is the value of $user['id'] , which is something that was set earlier (presumably the ID of the current user of the script). It then loops over all the rows returned by the query.

$comenziArray is an array that will contain the results.

It uses the comanda and id columns of the row.

comanda is a JSON object, which it decodes into an associative array. It contains an items array; if this array is empty, it skips this row.

It adds the id column from the row as an id element of the associative array.

Then the associative array is added to $comenziArray .

After the loop is finished, it converts $comenziArray to JSON and returns that to the caller.

To summarize, it's returning a JSON array of all the comanda objects associated with the given user, skipping the ones where items is empty, and adding the id of the row into each corresponding object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM