• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] Model Update Fails

#1
Hi,

I'm trying to update 1 record in my table. But $this->update($id, $data) just fails.

I've tried every which way but ... well my frustration ...

My db (has aprox 50 records and player_id 12 does exist)

TABLE `player_master` (
  `player_id` int(10) unsigned NOT NULL,
  `first_name` varchar(50) NOT NULL,
  `last_name` varchar(50) NOT NULL,
  `image_path` varchar(200) NOT NULL,
  `telephone` varchar(15) DEFAULT NULL,
  `email` varchar(50) DEFAULT NULL,
  `handicap` int(3) NOT NULL,
  PRIMARY KEY (`player_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


My model
PHP Code:
namespace App\Models\Admin;

use 
CodeIgniter\Model;

class 
Players extends Model {
    
    protected 
$table      'player_master';
    protected $primaryKey 'player_id';

    protected $returnType 'array';

    function 
player ($id) {
        return 
$this->where('player_id'$id)
        ->
find();
    }
    
    function 
player_update($id$data) {
        return    
$this->update($id$data);
    }

My code
PHP Code:
$players_func = new \App\Models\Admin\Players;                
$this->$data['player_name'] = $players_func->player($id);

//Player Data returned OK

$id=12;
$data=[
    
'image_path'=>'path_to_my_image'
    
];


$this-$data['update']= $players_func->player_update($id$data);

//This just fails - no error, just fails 

Note: the function player_update does not receive the var $id and $data.


Please help I am at a total loss ....

OK, so after thinking I'm going mad (more mad). I think I've sussed this out.

By defining allowed fields in the class, the update seems to have worked OK.

My model now reads
PHP Code:
namespace App\Models\Admin;

use 
CodeIgniter\Model;

class 
Players extends Model {
    
    
protected $table      'player_master';
    protected $primaryKey 'player_id';
    
    
protected $returnType 'array';
    protected $allowedFields = ['image_path'];

    function player ($id) {
        return $this->where('player_id'$id)
        ->find();
    }

meaning I can call update directly, without having to create a function,

ie.

PHP Code:
$players_func = new \App\Models\Admin\Players;                
$this
->$data['player_name'] = $players_func->player($id);

//Player Data returned OK

$id=12;
$data=[
    'image_path'=>'path_to_my_image'
    ];


$this-$data['update']  $players_func->update($id$data); 

Hope this help others
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.