CodeIgniter Forums
Inserting/Updating "Userstamps" from a Model - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Inserting/Updating "Userstamps" from a Model (/showthread.php?tid=77335)

Pages: 1 2


RE: Inserting/Updating "Userstamps" from a Model - Matleyx - 12-21-2020

YES!
It's works fine!
Thanks a lot


RE: Inserting/Updating "Userstamps" from a Model - sknair143 - 08-31-2023

My Model :


Code:
<?php

namespace App\Models;

use CodeIgniter\Model;

class EmployeesMasterModel extends CustomModel
{

    protected $table = 'employees_master';
    protected $returnType = '\App\Entities\EmployeesEntity';
    protected $useSoftDeletes = true;
    protected $allowedFields = [];

    // Dates
    protected $useTimestamps = true;


    // Validation
    protected $validationRules = [];
    protected $validationMessages = [];

    // Callbacks
    protected $allowCallbacks = true;
    protected $beforeInsert = ['insertUserstamp'];
    protected $beforeUpdate = ['updateUserstamp'];


}
?>





My Custom Model :

Code:
<?php

namespace App\Models;

use CodeIgniter\Model;

class CustomModel extends Model
{
    protected function insertUserstamp(array $data)
    {
        $user_id = session()->get('user_id');
        if (
            !empty($user_id) &&
            !array_key_exists('created_by', $data) && !array_key_exists('updated_by', $data)
        ) {
            $data['data']['created_by'] = $user_id;
            $data['data']['updated_by'] = $user_id;
        }
        return $data;
    }

    /**
    * This method saves the session "user_id" value to "updated_by" array element before
    * the row is inserted into the database.
    *
    */
    protected function updateUserstamp(array $data)
    {
        $user_id = session()->get('user_id');
        if (!empty($user_id) && !array_key_exists('updated_by', $data)) {
            $data['data']['updated_by'] = $user_id;
        }
        return $data;
    }
}
?>


But the field not getting updated in my table. The field setting as NULL


RE: Inserting/Updating "Userstamps" from a Model - InsiteFX - 08-31-2023

PHP Code:
// Add the fields to the 
$allowedFields['created_by'updated_by]; 



RE: Inserting/Updating "Userstamps" from a Model - sknair143 - 08-31-2023

(08-31-2023, 02:13 AM)InsiteFX Wrote:
PHP Code:
// Add the fields to the 
$allowedFields['created_by'updated_by]; 


Its fixed. i changed

Code:
session->get('user_id');

to

session->get('users.id');



RE: Inserting/Updating "Userstamps" from a Model - InsiteFX - 08-31-2023

You can also use the session helper like this:

PHP Code:
session()->get('key');

$data = [
    'key' => 'value',
];

session()->set($data);
//or key/value
session()->set('key''value'); 



RE: Inserting/Updating "Userstamps" from a Model - InsiteFX - 09-01-2023

What your doing is extending the CodeIgniter Model into a BaseModel.

Then every new Model that you create you extend it with the new BaseModel that yoou created earlier.



PHP Code:
<?php

namespace App\Models;

use 
CodeIgniter\Model;
/**
 * Extend the CodeIgniter Model
 */ 
class BaseModel extends Model
{
    // all new methods will go in here!
}

-------------------------------------------------

<?
php

namespace App\Models;

use 
App\Models\BaseModel;

/**
 * Create a new Model & extend all new Models from BaseModel
 */
class NewModel extends BaseModel
{