Files

43 lines
1.1 KiB
PHP

<?php
defined('BASEPATH') or exit('No direct script access allowed');
class StationModel extends MY_Model
{
private $table = 'cm_station';
public function __construct()
{
parent::__construct();
$this->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];;
}
}