CodeIgniter Forums
my model does not work as expected - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forum-10.html)
+--- Thread: my model does not work as expected (/thread-76932.html)



my model does not work as expected - Ahmad Ali - 07-04-2020

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;
    }




RE: my model does not work as expected - marcogmonteiro - 07-05-2020

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.


RE: my model does not work as expected - IvanGlickman - 04-15-2021

(07-05-2020, 10:50 AM)marcogmonteiro Wrote: 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 buy.
Thank you! I see how it works now.


RE: my model does not work as expected - Codinglander - 04-15-2021

You can also use this:

http://codeigniter.com/user_guide/models/model.html#protecting-fields

For updating data this works perfectly, too.