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

ok, ho creato questo CustomModel.php:
PHP Code:
<?php

namespace App\Models;

use 
CodeIgniter\Model;


class 
CustomModel extends Model
{
    /**
     * Class properties go here.
     * -------------------------------------------------------------------
     *
     * public, private, protected, static and const.
     */


    /**
     * Name of database table
     *
     * @var string
     */
    protected $table '';

    /**
     * The format that the results should be returned as.
     * Will be overridden if the as* methods are used.
     *
     * @var string
     */
    protected $returnType 'array';


    /**
     * An array of field names that are allowed
     * to be set by the user in inserts/updates.
     *
     * @var array
     */
    protected $allowedFields = [];

    /**
     * __construct ()
     * -------------------------------------------------------------------
     *
     * Class    Constructor
     *
     * NOTE: Not needed if not setting values or extending a Class.
     *
     */
    public function __construct()
    {
        parent::__construct();
    }

    // -----------------------------------------------------------------------
  /**
     * This method saves the session "user_id" value to "created_by" and "updated_by" array 
     * elements before the row is inserted into the database.
     *
     */
    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;
    
}
   // End of YourModelName Model Class.

/**
 * -----------------------------------------------------------------------
 * Filename: YourModelName.php
 * Location: ./app/Models/YourModelName.php
 * -----------------------------------------------------------------------
 */ 


But do I also have to modify the standard model to run these functions?
Otherwise how do I call them in insert and updates?
Reply


Messages In This Thread
RE: Inserting/Updating "Userstamps" from a Model - by Matleyx - 12-20-2020, 10:18 AM



Theme © iAndrew 2016 - Forum software by © MyBB