nhance/app/Models/UserModel.php

102 lines
2.7 KiB
PHP

<?php
namespace App\Models;
use CodeIgniter\Model;
class UserModel extends Model
{
protected $DBGroup = 'default';
protected $table = 'user_profiles';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $insertID = 0;
protected $returnType = 'array';
protected $useSoftDelete = false;
protected $protectFields = true;
protected $allowedFields = [
"id",
"emp_code",
"first_name",
"last_name",
"email",
"mobile",
"profile",
"role",
"created_by",
"created_at",
"updated_by",
"updated_at",
"created_by",
"updated_by",
"is_active",
];
// 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 = ["beforeInsert"];
protected $afterInsert = [];
protected $beforeUpdate = [];
protected $afterUpdate = [];
protected $beforeFind = ["removePassword"];
protected $afterFind = ["removePassword"];
protected $beforeDelete = [];
protected $afterDelete = [];
protected function beforeInsert(array $data)
{
$data = $this->passwordHash($data);
return $data;
}
protected function removePassword(array $data)
{
// $data =
unset($data['data']['password']);
return $data;
}
protected function passwordHash(array $data)
{
if (isset($data['data']['password'])) {
$data['data']['password'] = password_hash($data['data']['password'], PASSWORD_DEFAULT);
}
return $data;
}
public function getUserByEmail($email){
$userData = $this->where('email', $email)->get();
if ($userData->getNumRows() > 0) {
return $userData->getRow();
} else {
return false;
}
}
public function getUserById($id){
$userData = $this->where('id', $id)->get();
if ($userData->getNumRows() > 0) {
return $userData->getRow();
} else {
return false;
}
}
}
?>