vb_book/app/Models/EventModel.php
2024-12-09 17:03:24 +05:30

67 lines
2.1 KiB
PHP
Executable File

<?php
namespace App\Models;
use CodeIgniter\Model;
class EventModel extends Model
{
protected $table = 'events';
protected $primaryKey = 'event_id';
protected $allowedFields = ['event_id','event_name','created_by','created_on','updated_on' ,'updated_by','business_id','is_the_default','isactive'];
public function getEventnamesByBusiness($business_id)
{
$this->builder()->where('business_id ', $business_id); // Filter by business_id
$query = $this->builder()->get(); // Use findAll() instead of get()
return ($query->getResult());
}
public function setTable($tableName)
{
$this->table = $tableName;
return $this;
}
public function insertData($table, $data)
{
try {
$this->db->table($table)->insert($data);
$last_insert_id = $this->db->insertID();
return ["info"=>$last_insert_id,"err"=>""];
} catch (\CodeIgniter\Database\Exceptions\DatabaseException $e) {
return ["info"=>"","err"=>'Database operation failed: ' . $e->getMessage()];
} catch (\Exception $e) {
return ["info"=>"","err"=>'An error occurred: ' . $e->getMessage()];
}
}
public function updateData($table, $data, $where)
{
try {
$this->db->table($table)->update($data, $where);
$affected_rows = $this->db->affectedRows();
return ["info"=>$affected_rows." Affected","err"=>""];
} catch (\CodeIgniter\Database\Exceptions\DatabaseException $e) {
return ["info"=>"","err"=>'Database operation failed: ' . $e->getMessage()];
} catch (\Exception $e) {
return ["info"=>"","err"=>'Database operation failed: ' . $e->getMessage()];
}
}
public function getData($table, $where = null, $whereIn = null)
{
$query = $this->db->table($table);
if ($where) {
$query->where($where);
}
if ($whereIn) {
// $query->whereIn($column_name,$missingValues)
$query->whereIn($whereIn[0], $whereIn[1]);
}
return $query->get()->getResult();
}
}
?>