load->database(); } private function findByCategoryConfigured() { return $this->db->where('Category >', 0)->select('Id, Category, Title')->get('cm_station')->result(); } public function save($stations) { $categorizedStations = $this->findByCategoryConfigured(); $this->db->where('Category <=', 0)->delete($this->table); $inserted = array(); $updated = array(); foreach ($stations as $key => $station) { $upd = false; foreach ($categorizedStations as $c) { if ($c->Title == $station->Title) { $stations[$key]->Category = $c->Category; $upd = true; $this->db->where($c->Id)->update($this->table, $station); array_push($updated, $c->Id); } } if (!$upd) { $this->db->insert($this->table, $station); array_push($inserted, $this->db->insert_id()); } } return ['inserted' => $inserted, 'updated' => $updated];; } }