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

(05-27-2024, 11:16 AM)ozornick Wrote: You can change the fill() method by filtering the array_filter() array of $data and allowing only $attributes from the model

Many thanks. I did not know that. Now to study array_filter(). Thank you!
Reply
#12

(This post was last modified: 05-28-2024, 05:19 AM by spreaderman.)

(05-28-2024, 04:16 AM)spreaderman Wrote:
(05-27-2024, 11:16 AM)ozornick Wrote: You can change the fill() method by filtering the array_filter() array of $data and allowing only $attributes from the model

Many thanks.  I did not know that.  Now to study array_filter().  Thank you!

Something like this;

PHP Code:
$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);
          
          echo 
'***this can go into the db if hasChanged()***';  
          echo 
print_r($attributes_matching_from_new_fill).'</br>'

where
attributes_matching_from_new_fill - is the new associative array that matches $attribute ($attributes = $this->OsmSkiResortModel->allowedFields) and $new_fill is the unfiltered associative array of the submitted data.

Seems to work.

Thank you.
Reply
#13

(This post was last modified: 06-02-2024, 03:43 AM by spreaderman.)

I thought it was working but not so. I am getting the following error:

Error
Call to a member function fill() on null

It is related to:
APPPATH/Controllers/Admin/Ski/Ski_Controller.php at line 312

I have highlight line 312 below which is indeed fill()


PHP Code:
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();
            
$OsmSkiResort $this->OsmSkiResortModel->find($new_fill['id']);
            
            
//get list all permissable attrobutes that can go into the db
            
$attributes $this->OsmSkiResortModel->allowedFields;
           
            
// filter new attroibutes 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);
          
            
// $attributes_matching_from_new_fill is what can go into the db
            
            
echo '<pre>';
            
print_r($attributes_matching_from_new_fill);
            echo 
'</pre>';
            
312            $OsmSkiResort->fill($attributes_matching_from_new_fill);
            
            if (
$OsmSkiResort->hasChanged()){
                
$this->OsmSkiResortModel->save($OsmSkiResort->fill($new_fill));
                echo 
'<pre>';
           
                
$hasChanged++;
                
// will show what has changed
                
print_r(  $OsmSkiResort->toRawArray(true));
            
                echo 
'</pre>';
                
            } else {
                
$nothingToUpdate++;
            }
            
$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 output of the $attributes_matching_from_new_fill indeed returns the below:

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


In line 294, actually nothing is returned because it is new data to be inserted. I though if there was no existing data save() would insert or delete.

Any help here much appreciated.
Reply
#14

Why double fill() ? 
PHP Code:
$this->OsmSkiResortModel->save($OsmSkiResort->fill($new_fill)); 
Simple CI 4 project for beginners codeigniter-expenses
Reply
#15

(This post was last modified: 06-04-2024, 01:23 AM by spreaderman.)

(06-03-2024, 12:09 AM)ozornick Wrote: Why double fill() ? 
PHP Code:
$this->OsmSkiResortModel->save($OsmSkiResort->fill($new_fill)); 

Thanks @ozornick. I can change that but it still fails at:

PHP Code:
$OsmSkiResort->fill($attributes_matching_from_new_fill); 

with the error
PHP Code:
"Call to a member function fill() on null" 

I tested attributes_matching_from_new_fill (see code above) and it contains this array:

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


so it is puzzling what fill() fails.
Reply
#16

Maybe I am not understanding ENTITIES properly?

The model is setup to returntype ENTITY.

In my controller, do I need to load both the ENTITY and the MODEL? I am kind of confused as to when I need to use the model class or the entity class.

use App\Models\QgisSkiResortModel;
use App\Entities\QgisSkiResortEntity;

For CRUD operations, if the below correct?

example CREATE/UPDATE
==============
For "create" or "update"

If i used the model it would be something like;
$userModel = new \App\Models\UserModel();
$userModel->insert($data);

but if I used entity, would it be bellow same as above like:
$userEntity = new \App\Models\UserEntity;
$userEntity->insert($data);

Not getting it. Sad
Reply
#17

See my pet project)

https://github.com/neznaika0/codeigniter...penses.php

and filter properties
https://github.com/neznaika0/codeigniter...el.php#L55
The model works with an entity. In this case, you need to create two objects:

PHP Code:
$entity new UserEntity([...]);
$userModel = new \App\Models\UserModel();
$userModel->insert($entity); 
Simple CI 4 project for beginners codeigniter-expenses
Reply
#18

(05-25-2024, 09:42 PM)InsiteFX Wrote: 1) For one change to Camel Case.
PHP Code:
update_aa_winter_sports_points_from_file()

// Change to Camel Case makes it easier to read.
updateAaWinterSportsPointsFromFile() 

To me, "update_aa_winter..." is easier...  Smile

(05-25-2024, 09:42 PM)InsiteFX Wrote: 2) You have an extra closing bracket on your if/else statement.
PHP Code:
if (!$original_OsmSkiResort->hasChanged()){
echo 
"no change on record number ".$i.'</br>';
// <-- Extra closing Bracket remove!

} else {
$y $this->OsmSkiResortModel->save($OsmSkiResort) ;
echo 
"save: ".$y.'</br>';



I think the "} else {" belongs to the prior line 
The lack of indentation was the problem...  Smile

PHP Code:
if ($original_OsmSkiResort){ 
Reply
#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
#20

Read the error message carefully.
"Call to a member function fill() on null"
That is, $QgisResort is null.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB