102 lines
2.9 KiB
PHP
Executable File
102 lines
2.9 KiB
PHP
Executable File
<?php
|
||
|
||
namespace App\Models;
|
||
|
||
use CodeIgniter\Model;
|
||
|
||
class PartnerStaffModel extends Model
|
||
{
|
||
protected $table = 'partner_staff'; // table name
|
||
protected $primaryKey = 'id'; // primary key
|
||
|
||
protected $useAutoIncrement = true;
|
||
|
||
// return results as array
|
||
protected $returnType = 'array';
|
||
protected $useSoftDeletes = false;
|
||
|
||
// allowed fields for insert/update
|
||
protected $allowedFields = ['name','email','mobile','emp_id','role_id','email_otp','is_active','created_by','updated_by','manager_id'];
|
||
|
||
// automatic timestamps
|
||
protected $useTimestamps = true;
|
||
protected $createdField = 'created_on';
|
||
protected $updatedField = 'updated_on';
|
||
protected $dateFormat = 'datetime';
|
||
|
||
// validation rules
|
||
// protected $validationRules = [
|
||
// 'name' => 'required|min_length[2]|max_length[150]',
|
||
// 'email' => 'permit_empty|valid_email|max_length[150]',
|
||
// 'mobile' => 'permit_empty|regex_match[/^[0-9]{10,20}$/]',
|
||
// ];
|
||
|
||
// protected $validationMessages = [
|
||
// 'name' => [
|
||
// 'required' => 'Name is required',
|
||
// 'min_length' => 'Name must have at least 2 characters',
|
||
// 'max_length' => 'Name cannot exceed 150 characters'
|
||
// ],
|
||
// 'email' => [
|
||
// 'valid_email' => 'Please provide a valid email address',
|
||
// 'max_length' => 'Email cannot exceed 150 characters'
|
||
// ],
|
||
// 'mobile' => [
|
||
// 'regex_match' => 'Mobile number must be 10–20 digits only'
|
||
// ]
|
||
// ];
|
||
|
||
// protected $skipValidation = false;
|
||
public function updateByKey($key)
|
||
{
|
||
$db = \Config\Database::connect();
|
||
$message = "";
|
||
|
||
// Start transaction
|
||
$db->transStart();
|
||
|
||
try {
|
||
// 1. Update manager
|
||
$db->table('partner_staff')
|
||
->where('id', $key)
|
||
->set('is_active', 0)
|
||
->update();
|
||
$managerRows = $db->affectedRows();
|
||
$message = "{$managerRows} manager(s), ";
|
||
|
||
// 2. Update agents under manager
|
||
$db->table('partner_agent')
|
||
->where('manager_id', $key)
|
||
->set('is_active', 0)
|
||
->update();
|
||
$agentRows = $db->affectedRows();
|
||
$message .= "{$agentRows} agent(s), ";
|
||
|
||
// 3. Update staff under manager
|
||
$db->table('partner_staff')
|
||
->where('manager_id', $key)
|
||
->set('is_active', 0)
|
||
->update();
|
||
$staffRows = $db->affectedRows();
|
||
$message .= "{$staffRows} staff(s)";
|
||
|
||
// Complete transaction
|
||
$db->transComplete();
|
||
|
||
// Check transaction status
|
||
if ($db->transStatus() === false) {
|
||
return "Transaction failed. No updates were applied.";
|
||
}
|
||
|
||
return $message . " are deleted successfully";
|
||
|
||
} catch (\Exception $e) {
|
||
// Rollback in case of exception
|
||
$db->transRollback();
|
||
return "Error: " . $e->getMessage();
|
||
}
|
||
}
|
||
|
||
|
||
}
|