public function saveAction() { //read the json format post from angularjs $postdata = file_get_contents("php://input"); $post = json_decode($postdata, true); $map = $post['map']; $target = $post['target']; $password = $post['password']; $status = $post['status']; //read file from the upload directory //send 'true' as all entries should be processed $data = $this->readFile($target); if (array_key_exists('error', $data) || !array_key_exists('data', $data)) { $error = ['error' => 'File is corrupted']; $this->_helper->json($error); exit; } $data = $this->sanitize($data['data']); $pointer = 0; $counter = 0; try { foreach ($data as $user) { //combine with keys from mapping $user = array_combine($map, $user); //set up doctrine model $model = new Application_Model_Users(); //if required data is missing, return error if (empty($user['firstname']) || empty($user['lastname']) || empty($user['email'])) { $error = ['error' => 'Required Field is missing from data']; $this->_helper->json($error); exit; } //populate required field $model->setFirstname($user['firstname']); $model->setLastname($user['lastname']); $model->setEmail($user['email']); //fillup password and status. If not found, enter with default value if (!empty($user['password'])) { $model->setPassword($user['password']); } else { $model->setPassword($password); } if (!empty($user['status'])) { $model->setStatus($user['status']); } else { $model->setStatus($status); } //populate optional fields: optional fields. check both keys and values if (array_key_exists('country', $user) && $user['country']) { $model->setCountry($user['country']); } if (array_key_exists('city', $user) && $user['city']) { $model->setCity($user['city']); } if (array_key_exists('address', $user) && $user['address']) { $model->setAddress($user['address']); } //persist the data $this->em->persist($model); $pointer++; $counter++; //bulk saving by 20 if ($pointer == 20) { $pointer = 0; $this->em->flush(); } } //final batch save $this->em->flush(); } catch (Exception $e) { $error = ['error' => 'Database entry failed']; $this->_helper->json($error); exit; } $this->_helper->json(['success' => $counter]); exit; }