How to understand entities, save, hasChanged |
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>'; } }
1) For one change to Camel Case.
PHP Code: update_aa_winter_sports_points_from_file() 2) You have an extra closing bracket on your if/else statement. PHP Code: if (!$original_OsmSkiResort->hasChanged()){ What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(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. I do not see any extra brace. That would surely throw an error.
(05-25-2024, 10:53 PM)kenjis Wrote: Read https://codeigniter.com/user_guide/model...attributes Thank you Kenjis. I previously read that. The below code works but I dont understnad why. In line 3 below, I used find() and it returns a $category entity. Is line 3 required in order for line 5 to work properly? How does hadChanged() understand it changed? PHP Code: 1 public function category_update($category_id) going back to my original problem, I am readying a geojson from file. I put it into an associative array. On the first run of the script, everything imports to the db as expected. If I changed something in the geojson text file, I want to just save the updated parts. Unfortunately, the code says every record hasChanged. I have changed around my code to try to understand what is ocurring but I cannot see why also see as hasChanged. PHP Code: public function update_aa_winter_sports_points_from_file(){ And here is the output: Numnber of ski resorts: 223 ---------------------------------- File row number 0 Data has changed This is what CI says has changed: Array ( [landuse] => winter_sports [name] => チセヌプリスキー場 [name:en] => Chisennupri [name:ja] => 蘭越町チセヌプリスキー場 [piste:type] => playground [sport] => skiing [type] => multipolygon [id] => relation/8385428 ) This is what is in the database App\Entities\OsmSkiResortEntity Object ( [snip]
See your code here EXTRA BRACKET on if else statement
PHP Code: if (!$original_OsmSkiResort->hasChanged()){ What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(05-26-2024, 02:12 AM)spreaderman Wrote: In line 3 below, I used find() and it returns a $category entity. Is line 3 required in order for line 5 to work properly? How does hadChanged() understand it changed? Yes, it is required. The hadChanged() checks if the original data and the current data are the same. The original data is set when finding the entity from the database.
(05-26-2024, 05:53 PM)kenjis Wrote:(05-26-2024, 02:12 AM)spreaderman Wrote: In line 3 below, I used find() and it returns a $category entity. Is line 3 required in order for line 5 to work properly? How does hadChanged() understand it changed? Thank you Kenjis. Beginning to make sense now. So kind of like below? PHP Code: $category = $this->CategoryModel->find($category_id); // so $category contains a category object/entity if found. I very much appreciate your and everyone's help!
I think I figured it out. Indeed the data and the new data were different and hasChanged() was of course correct.
I have 25 fields in the databases. Sometimes the input data is 28 fields (some new data I dont need). Anyway to tell the entity to ignore extra fields? I think this is the source of the error. Thank you.
You can change the fill() method by filtering the array_filter() array of $data and allowing only $attributes from the model
Simple CI 4 project for beginners codeigniter-expenses
|
Welcome Guest, Not a member yet? Register Sign In |