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

Thank you Kenjis.

It now works.

Something like this right? It now saves on new data, updates and doesnt do anthing if the record is not changed.

PHP Code:
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);
            
            
// create a new record
            
if ($QgisResort === null){
                
$newSkiResort = new \App\Entities\QgisSkiResortEntity();
                
$newSkiResort->fill($attributes_matching_from_new_fill);
                
$this->QgisSkiResortModel->save($newSkiResort);
                
$resortNew++;
            
// record exists
            
} elseif($QgisResort){
                
$QgisResort->fill($attributes_matching_from_new_fill);
                if (
$QgisResort->hasChanged()){
                    if (
$this->QgisSkiResortModel->save($attributes_matching_from_new_fill)){
                        
$resortUpdated++;   
                    }
                }
                if (!
$QgisResort->hasChanged()){
                    
$resortNothingToUpdate++;
                    echo 
$QgisResort->id.'</br>';
                    echo 
$QgisResort->{'name:en'}.'</br>';
                    echo 
'</br>';
                }
            }
            
            
$i++;
        } 
Reply


Messages In This Thread
RE: How to understand entities, save, hasChanged - by spreaderman - 06-07-2024, 09:48 PM



Theme © iAndrew 2016 - Forum software by © MyBB