1

Right now, we're in the process of moving our frontend to AngularJS. I'm fetching all users with a model Users. Right after doing that, I'm passing the data to AngularJS. This is the code:

$users = Users::find();

$users_array = array();

foreach ($users as $user) {

    if ($user->user) {
        $birthDate = explode("-", $user->user->getBirthdate());

        if (count($birthDate) > 2) {
            $age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) > date("md")
                    ? ((date("Y") - $birthDate[2]) - 1)
                    : (date("Y") - $birthDate[2]));
         } else {
             $age = "";

         }

         $array = array('id' => $user->user->getId(), 'firstname' => $user->user->getFirstname(), 'lastname' => $user->user->getLastname(), 'birthDate' => $age, 'gender' => $user->user->getGenderNeat());
         array_push($users_array, $array);
    }
}

return $users_array;

This isn't very efficient, because I'm looping over all users in the database. I do need to do something similar to this, because I can't query the models in AngularJS. Are there any better options? If so, how can I do it?

2
  • Have you considered paginating Users? Commented Dec 21, 2015 at 12:14
  • echo json_encode($users_array); then pass it to Angular using this method stackoverflow.com/questions/13020821/… Commented Dec 28, 2015 at 19:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.