Welcome Guest, Not a member yet? Register   Sign In
Inserting/Updating "Userstamps" from a Model
#11

YES!
It's works fine!
Thanks a lot
Reply
#12

(This post was last modified: 08-31-2023, 12:56 AM by sknair143. Edit Reason: formating )

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
Reply
#13

PHP Code:
// Add the fields to the 
$allowedFields['created_by'updated_by]; 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#14

(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');
Reply
#15

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'); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#16

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
{


What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB