nhance/app/Models/MessageModel.php

77 lines
2.2 KiB
PHP

<?php
namespace App\Models;
use CodeIgniter\Model;
class MessageModel extends Model
{
protected $table = 'messages';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = 'array';
protected $useSoftDeletes = false;
protected $protectFields = true;
protected $allowedFields = [
'id',
'message_text',
'user_id',
'role_id',
'team_id',
'message_type',
'created_at',
'action_url',
];
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 getMessagesForUser($userId, $roleId, $teamId)
{
$builder = $this->db->table($this->table);
$builder->select('messages.*');
$builder->join('user_messages', 'user_messages.message_id = messages.id AND user_messages.user_id = ' . $this->db->escape($userId), 'left');
$builder->groupStart();
$builder->where('user_messages.is_read', null);
$builder->orWhere('user_messages.is_read', 0);
$builder->groupEnd();
$builder->groupStart();
$builder->orwhere('messages.message_type', 'broadcast');
$builder->orWhere('messages.user_id', $userId);
$builder->orWhere('messages.role_id', $roleId);
$builder->orWhere('messages.team_id', $teamId);
$builder->groupEnd();
$query = $builder->get();
return $query->getResult();
dd($this->db->getLastQuery());
}
}