nhance/app/Models/PartnerPosModel.php
2025-12-19 11:33:16 +05:30

135 lines
3.8 KiB
PHP

<?php namespace App\Models;
use CodeIgniter\Model;
class PartnerPosModel extends Model
{
// 1. Core Configuration
/**
* @var string The database table that this model primarily works with.
*/
protected $table = 'partner_pos';
/**
* @var string The name of the primary key field.
*/
protected $primaryKey = 'id';
/**
* @var string The type of value the primary key is.
*/
protected $returnType = 'array'; // Can be 'array' or 'object' (e.g., \App\Entities\PartnerPos)
/**
* @var bool Whether to use timestamps. We will handle them manually with $useTimestamps.
*/
protected $useAutoIncrement = true;
// 2. Timestamps and Dates
/**
* @var bool Whether to use the automatic timestamps. Set to true if you use CI4's
* default `created_at` and `updated_at` column names. Since you use custom names,
* we set this to false and handle the fields manually in $allowedFields.
*/
protected $useTimestamps = true;
/**
* @var string The field name for the creation timestamp.
* We use `created_on` to match your table.
*/
protected $createdField = 'created_on';
/**
* @var string The field name for the update timestamp.
* We use `updated_on` to match your table.
*/
protected $updatedField = 'updated_on';
// Note: The `created_by` and `updated_by` fields need to be handled
// in the controller/service layer before calling insert/update.
// 3. Allowed Fields
/**
* @var array The fields that can be set during an insert or update operation.
*/
protected $allowedFields = [
'name',
'email',
'mobile',
'pos_code',
'certificate_file_name',
'gst','pan',
'pan_file_name',
'aadhar',
'aadhar_file_name',
'is_active',
'updated_on',
'created_by',
'updated_by',
'manager_id',
'bank_name',
'account_holder_name',
'account_number',
'ifsc_code',
'address',
'state',
'city',
'pincode',
// 'created_on' and 'updated_on' are handled by the Model,
// but need to be in $allowedFields if $useTimestamps is false.
// Since $useTimestamps is true and we customized the field names, CI4 handles them.
];
// 4. Validation (Optional, but highly recommended in CI4)
// protected $validationRules = [
// 'name' => 'required|min_length[3]',
// 'email' => 'permit_empty|valid_email|is_unique[partner_pos.email,id,{id}]',
// 'mobile' => 'permit_empty|numeric|max_length[20]|is_unique[partner_pos.mobile,id,{id}]',
// 'manager_id' => 'required|integer',
// ];
// protected $validationMessages = [
// 'email' => [
// 'is_unique' => 'Sorry, that email address is already registered.',
// ],
// ];
/**
* @var bool Whether to skip validation during inserts and updates.
*/
protected $skipValidation = false;
/**
* @var bool Whether to clean data for SQL Injection.
*/
protected $cleanValidationRules = true;
protected $beforeInsert = ["checkAndADDCreatedByValue"];
protected $beforeUpdate = ["checkAndUpdateUpdatedByValue"];
protected function checkAndADDCreatedByValue(array $data)
{
if (empty($data['data']['created_by'])) {
$data['data']['created_by'] = get_session_userid();
}
return $data;
}
protected function checkAndUpdateUpdatedByValue(array $data)
{
if (empty($data['data']['updated_by'])) {
$data['data']['updated_by'] = get_session_userid();
}
$data['data']['updated_on'] = date('Y-m-d H:i:s');
return $data;
}
}