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

(This post was last modified: 06-07-2024, 05:25 PM by spreaderman.)

Thank you for all the pointer thus far! Really appreciated. Really having a hard time understanding entities. I still get an error Call to a member function fill()

PHP Code:
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);
    
}



The above code still fail at $QgisResort->fill($attributes_matching_from_new_fill); and says "Call to a member function fill() on null", however, when I echo out $attributes_matching_from_new_fill I have the following;

PHP Code:
Array ( 
  [
contact:website] => http://www.theboon.net/opas/ 
  
[landuse] => winter_sports 
  
[name] => 太平山スキー場 オーパス 
  
[name:en] => Taiheizan Ski Area Opus 
  
[wikidata] => Q11443946 
  
[id] => relation/5466186 

Reply


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



Theme © iAndrew 2016 - Forum software by © MyBB