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

(06-07-2024, 06:51 PM)kenjis Wrote: Read the error message carefully.
"Call to a member function fill() on null"
That is, $QgisResort is null.

Thank you Kenjis. It is my lack of understanding of entities. I thought that if QgisResort was empty and I fill it, it will treat it as new if I save it. Do I need to fill $QgisResort and then save? How should this work? Thank you again.
Reply
#22

It seems you misunderstand something.

PHP Code:
$QgisResort $this->QgisSkiResortModel->find($new_fill['id']); 

The Model::find() method returns null if the entity is not found.
Reply
#23

(06-07-2024, 07:39 PM)kenjis Wrote: It seems you misunderstand something.

PHP Code:
$QgisResort $this->QgisSkiResortModel->find($new_fill['id']); 

The Model::find() method returns null if the entity is not found.

I am trying to:
- insert new data if the entity doesn't exist
- update existing data if the entity exists

I am confused how to use find(), fill() save() hasChanged() to accomplish above. Very sorry but what is the basis logic to accomplish this?

I understand find(). It will be null if not found and means I should create a new entity to save. After that do I create a new entity and fill() to it and then save()?

If find() finds something, do I fill that entity object with fill() and check thereafter if hasChanged()? and then if hasChanged() i save() it?

Thank you.
Reply
#24

(06-07-2024, 07:51 PM)spreaderman Wrote: I understand find().  It will be null if not found and means I should create a new entity to save.  After that do I create a new entity and fill() to it and then save()?

Yes.
Reply
#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




Theme © iAndrew 2016 - Forum software by © MyBB