diff --git a/app/Controllers/Location.php b/app/Controllers/Location.php index 82b3162..ce81c0c 100644 --- a/app/Controllers/Location.php +++ b/app/Controllers/Location.php @@ -19,8 +19,8 @@ class Location extends Controller { public function index() { $rows = $this->db->table('location l') ->select("l.LocationID, LocCode, Parent, LocFull, LocType, v.VDesc ") - ->join("locationaddress la", "l.LocationID=la.LocationID") - ->join("valueset v", "v.VSetID=12 and v.VValue=l.LocType") + ->join("locationaddress la", "l.LocationID=la.LocationID", 'left') + ->join("valueset v", "v.VSetID=12 and v.VValue=l.LocType", 'left') ->get()->getResultArray(); if (empty($rows)) { @@ -62,31 +62,22 @@ class Location extends Controller { } public function create() { + $input = $this->request->getJSON(true); + $dataLocation = $this->prepareLocationData($input); + $dataLocationAddress = $this->prepareLocationAddressData($input); + if (!$this->validateData($dataLocation, $this->rules)) { + return $this->failValidationErrors($this->validator->getErrors()); + } + try { - $input = $this->request->getJSON(true); - - // Prepare data - $dataLocation = $this->prepareLocationData($input); - $dataLocationAddress = $this->prepareLocationAddressData($input); - - if (!$this->validateData($dataLocation, $this->rules)) { - return $this->failValidationErrors($this->validator->getErrors()); - } - - // Start transaction $this->db->transStart(); - - // Insert location $this->db->table('location')->insert($dataLocation); $newLocationID = $this->db->insertID(); - - // Insert address if available + if (!empty($dataLocationAddress)) { $dataLocationAddress['LocationID'] = $newLocationID; $this->db->table('locationaddress')->insert($dataLocationAddress); } - - // Complete transaction $this->db->transComplete(); if ($this->db->transStatus() === false) {