• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
my model does not work as expected

#1
Hi, I have a users table in the database the problem is when I want to update a selected row in the table using save or update function it does not update.
I don't know why but I'm sure that the code is fine.
Below is the function that updates the selected row.
PHP Code:
public function edit_profile()
    {
        if ($this->request->getPost()) {
            $email $this->request->getPost('email');
            $username $this->request->getPost('username');
            $model = new UserModel();
            $site_session = new site_session();
            $id session()->get($site_session->userid());

            echo $id ' '$email ' ' $username;
            $model->save([
                'id' => $id,
                'email' => $email,
                'username' => $username
            
]);
        }
        //    return  redirect()->to('profile');
    
My model 
PHP Code:
<?php

namespace App\Models;

use 
CodeIgniter\Model;
use 
site_session;

class 
UserModel extends Model
{

    protected $table      'users';
    protected $primaryKey 'id';

    protected $returnType     'array';
    protected $useSoftDeletes true;

    protected $allowedFields = ['email''username''password'];

    protected $useTimestamps true;
    protected $createdField  'created_at';
    protected $updatedField  'updated_at';
    protected $deletedField  'deleted_at';

    protected $validationRules    = [
        'email' => 'required|valid_email',
        'username' => 'required|min_length[2]|max_length[10]|is_unique[users.username]',
        'password' => 'required|min_length[8]|max_length[20]'
    ];
    protected $validationMessages = [
        'email' => [
            'required' => 'This field is required',
            'valid_email' => 'email is not valid make sure you wrote it right'
        ],
        'username' => [
            'required' => 'This field is required',
            'min_length' => 'the min username is 2',
            'is_unique' => 'this username is takken',
            'max_length' => 'the max username is 10'
        ],
        'password' => [
            'required' => 'This field is required',
            'min_length' => 'the min pasword is 8',
            'max_length' => 'the max pasword is 20'
        ]
    ];
    // protected $skipValidation     = false;

    protected $beforeInsert = ['hashPassword'];
    protected $beforeUpdate = ['hashPassword'];

    protected function hashPassword(array $data)
    {
        if (!isset($data['data']['password']))
         return $data;

        $password $data['data']['password'];
        $data['data']['password'] = password_hash($passwordPASSWORD_DEFAULT);
        
    
        
return $data;
    }

    public function validateUser($data $password){
        if(password_verify($password $data)){
            return true;
        }
        if(password_verify($data $password)){
            return true;
        }
        return false;
    }

Reply

#2
Your password field is required but you're not setting a password when you call the save method. If you print the model errors after the save method you'll be able to see that.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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