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?