nhance/app/Models/UserMessageModel.php
2024-09-13 09:00:09 +05:30

70 lines
1.8 KiB
PHP
Executable File

<?php
namespace App\Models;
use CodeIgniter\Model;
class UserMessageModel extends Model
{
protected $table = 'user_messages';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = 'array';
protected $useSoftDeletes = false;
protected $protectFields = true;
protected $allowedFields = [
'id',
'message_id',
'user_id',
'is_read',
'read_at',
];
protected bool $allowEmptyInserts = false;
// Dates
protected $useTimestamps = false;
protected $dateFormat = 'datetime';
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
// Validation
protected $validationRules = [];
protected $validationMessages = [];
protected $skipValidation = false;
protected $cleanValidationRules = true;
// Callbacks
protected $allowCallbacks = true;
protected $beforeInsert = [];
protected $afterInsert = [];
protected $beforeUpdate = [];
protected $afterUpdate = [];
protected $beforeFind = [];
protected $afterFind = [];
protected $beforeDelete = [];
protected $afterDelete = [];
public function markAsRead($userId, $messageId)
{
$data = [
'is_read' => 1,
'read_at' => date('Y-m-d H:i:s')
];
$existingEntry = $this->where('user_id', $userId)
->where('message_id', $messageId)
->first();
if ($existingEntry) {
return $this->update($existingEntry['id'], $data);
} else {
$data['user_id'] = $userId;
$data['message_id'] = $messageId;
return $this->insert($data);
}
}
}