You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
141 lines
2.7 KiB
141 lines
2.7 KiB
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
class Stores extends Model
|
|
{
|
|
/**
|
|
* table name
|
|
* @var string
|
|
*/
|
|
protected $table = 'stores';
|
|
protected $primaryKey = 'id'; // Primary key column
|
|
protected $allowedFields = ['name', 'active'];
|
|
|
|
/**
|
|
* To get active store data
|
|
* @return array
|
|
*/
|
|
public function getActiveStore()
|
|
{
|
|
return $this->where('active', 1)->findAll();
|
|
}
|
|
|
|
/**
|
|
* To get store data by ID
|
|
* @param int $id
|
|
* @return array|object|null
|
|
*/
|
|
public function getStoresData(int $id = null)
|
|
{
|
|
if ($id) {
|
|
return $this->where('id', $id)->first();
|
|
}
|
|
|
|
return $this->findAll();
|
|
}
|
|
|
|
public function getStoresName(int $id = null)
|
|
{
|
|
|
|
$this->select('name');
|
|
|
|
if ($id !== null) {
|
|
|
|
$row = $this->where('id', $id)->first();
|
|
return $row ? $row['name'] : null;
|
|
}
|
|
|
|
$rows = $this->findAll();
|
|
return array_column($rows, 'name');
|
|
}
|
|
|
|
|
|
/**
|
|
* create store
|
|
* @param array $data
|
|
* @return bool|int|string
|
|
*/
|
|
public function createStore(array $data)
|
|
{
|
|
if ($data) {
|
|
return $this->insert($data);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* update stores
|
|
* @param array $data
|
|
* @param int $id
|
|
* @return bool
|
|
*/
|
|
public function updateStore(array $data, int $id)
|
|
{
|
|
if ($data && $id) {
|
|
return $this->update($id, $data);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* remove store
|
|
* @param int $id
|
|
* @return bool|\CodeIgniter\Database\BaseResult
|
|
*/
|
|
public function removeStore(int $id)
|
|
{
|
|
if ($id) {
|
|
return $this->delete($id);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* count total store
|
|
* @return int|string
|
|
*/
|
|
public function countTotalStores()
|
|
{
|
|
return $this->where('active', 1)->countAllResults();
|
|
}
|
|
|
|
//Select store by id
|
|
public function getStoreById(int $id) {
|
|
return $this->db->table('stores')
|
|
->select('name')
|
|
->where('id', $id)
|
|
->get()
|
|
->getRow(); // Utiliser getRow() pour retourner un seul objet et non un tableau
|
|
}
|
|
|
|
public function getIdStoreByName(string $name): int
|
|
{
|
|
$row = $this->db
|
|
->table('stores')
|
|
->select('id')
|
|
->where('LOWER(name)', strtolower($name))
|
|
->get()
|
|
->getRow();
|
|
|
|
if ($row) {
|
|
return (int) $row->id;
|
|
}
|
|
|
|
$this->db->table('stores')->insert([
|
|
'name' => $name,
|
|
'active' => true,
|
|
]);
|
|
|
|
return (int) $this->db->insertID();
|
|
}
|
|
|
|
public function getAllStores()
|
|
{
|
|
return $this->orderBy('name', 'ASC')->findAll();
|
|
}
|
|
|
|
|
|
}
|