use App\Models\QgisSkiResortModel;
use App\Entities\QgisSkiResortEntity;
class Ski_Controller extends Admin_Controller
{
public function __construct(){
parent::__construct();
$this->QgisSkiResortModel = new \App\Models\QgisSkiResortModel;
}
public function qgis_data_update_aa_winter_sports_points_from_file(){
$file = file_get_contents(WRITEPATH.'ski/qgis_data/aa_winter_sports_points.geojson');
$aa_winter_sports_points = json_decode($file); // The json_decode() function is used to decode or convert a JSON object to a PHP object.
$i = 0; // used to count the number of ski resorts
$hasChanged = 0;
$nothingToUpdate = 0;
foreach ($aa_winter_sports_points->features as $aa_winter_sports_point ){
// Convert stdClass object to associative array in php
// https://stackoverflow.com/questions/34428702/convert-stdclass-object-to-associative-array-in-php
$data = array($aa_winter_sports_point->properties);
$new_fill = json_decode(json_encode($data[0]), TRUE);
// find() will return an entity $existing_data. Works also in findAll();
$QgisResort = $this->QgisSkiResortModel->find($new_fill['id']);
//get list all permissable attributes that can go into the db
$attributes = $this->QgisSkiResortModel->allowedFields;
// filter new attributes in the new fill
$attributes_matching_from_new_fill = array_filter($new_fill, function ($value, $key) use ($attributes) {
if (in_array($key, $attributes)) {
return true;
}
}, ARRAY_FILTER_USE_BOTH);
>>> JUST FOR DEBUG print_r($attributes_matching_from_new_fill);die(); <<<<<<
>> FAILS HERE $QgisResort->fill($attributes_matching_from_new_fill);
if ($QgisResort->hasChanged()){
$hasChanged++;
$this->QgisSkiResortModel->save($QgisResort);
echo "changed";
echo '<br>';
} else {
$nothingToUpdate++;
echo '<pre>';
echo 'no change';
echo '<br>';
echo '</pre>';
}
$i++;
}
//return redirect()->to('/admin/ski/upload_fs_data')
// ->with('info', 'There are a total of '.$i.' resorts counted. </br>Total Updated/New: '.$hasChanged.'<br>Total Unchanged: '.$nothingToUpdate);
}
}