Welcome Guest, Not a member yet? Register   Sign In
How to understand entities, save, hasChanged
#4

(05-25-2024, 08:27 PM)spreaderman Wrote: I have using entities and having a bit of trouble understanding them.  I am importing ski resort data from a geojson file.  All of the 300 ski resorts imported nicely.  Now I am testing if there is an UPDATE to the geojson import file or there is a new record (INSERT).  My understand is that I can use hasChanged and SAVE (save will detect if update or insert) but I cannot get hasChanged to detect a change.  Here is my code.

    public function update_aa_winter_sports_points_from_file(){
   
        $file = file_get_contents(WRITEPATH.'upload_fs_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.
        // to count the number of ski resorts
        $i = 0;
   
        echo 'Numnber of ski resorts: '.count($aa_winter_sports_points->features);  echo '</br>';

        foreach ($aa_winter_sports_points->features as $aa_winter_sports_point ){
       
            $x = array($aa_winter_sports_point->properties);

            // Convert stdClass object to associative array in php
            // https://stackoverflow.com/questions/3442...ray-in-php
            $new_OsmSkiResort = json_decode(json_encode($x[0]), TRUE);
           
            // get the id of the ski resort
            $id = ($new_OsmSkiResort['id']);
            echo $id.'</br>';
           
            $original_OsmSkiResort = $this->OsmSkiResortModel->find($id);
           
            $OsmSkiResort = new \App\Entities\OsmSkiResortEntity();
           
            $OsmSkiResort->fill($new_OsmSkiResort);
           
            if ($original_OsmSkiResort){
               
                if (!$original_OsmSkiResort->hasChanged()){
    echo "no change on record number ".$i.'</br>';
                }
               
            } else {
                $y = $this->OsmSkiResortModel->save($OsmSkiResort) ;
    echo "save: ".$y.'</br>';
            }
   
            $i++;
            echo '</pre>';
        }
       
    }

I do not see any extra brace. That would surely throw an error.
Reply


Messages In This Thread
RE: How to understand entities, save, hasChanged - by spreaderman - 05-26-2024, 12:44 AM



Theme © iAndrew 2016 - Forum software by © MyBB