102 lines
3.0 KiB
PHP
102 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use CodeIgniter\Model;
|
|
|
|
class MotorMasterVehicleModel extends Model
|
|
{
|
|
protected $table = 'motor_master_vehicle';
|
|
protected $primaryKey = 'vehicle_code';
|
|
protected $returnType = 'array';
|
|
protected $useAutoIncrement = false;
|
|
protected $protectFields = true;
|
|
protected $allowedFields = [
|
|
'vehicle_code', 'make', 'model', 'variant', 'body_type', 'seating_capacity',
|
|
'power', 'cubic_capacity', 'gross_vehicle_weight', 'fuel_type', 'no_of_wheels',
|
|
'abs', 'air_bags', 'length_m', 'ex_showroom_price', 'price_year',
|
|
'production_status', 'manufacturing', 'vehicle_type', 'is_active', 'imported_at',
|
|
];
|
|
protected $useTimestamps = false;
|
|
|
|
public function findActive(string $vehicleCode): ?array
|
|
{
|
|
return $this->where('vehicle_code', $vehicleCode)->where('is_active', 1)->first();
|
|
}
|
|
|
|
public function search(string $q, int $limit = 30): array
|
|
{
|
|
$q = trim($q);
|
|
if ($q === '') {
|
|
return [];
|
|
}
|
|
|
|
$builder = $this->builder()->where('is_active', 1);
|
|
|
|
if (preg_match('/^\d{6,}$/', $q)) {
|
|
$builder->like('vehicle_code', $q, 'after');
|
|
} else {
|
|
$builder->groupStart()
|
|
->like('make', $q)
|
|
->orLike('model', $q)
|
|
->orLike('variant', $q)
|
|
->orLike('vehicle_code', $q)
|
|
->groupEnd();
|
|
}
|
|
|
|
return $builder
|
|
->orderBy('make')
|
|
->orderBy('model')
|
|
->orderBy('variant')
|
|
->limit($limit)
|
|
->get()
|
|
->getResultArray();
|
|
}
|
|
|
|
public function distinctMakes(?string $q = null, int $limit = 100): array
|
|
{
|
|
$builder = $this->builder()
|
|
->select('make')
|
|
->where('is_active', 1)
|
|
->groupBy('make')
|
|
->orderBy('make')
|
|
->limit($limit);
|
|
|
|
if ($q) {
|
|
$builder->like('make', $q);
|
|
}
|
|
|
|
return array_column($builder->get()->getResultArray(), 'make');
|
|
}
|
|
|
|
public function distinctModels(string $make, ?string $q = null, int $limit = 200): array
|
|
{
|
|
$builder = $this->builder()
|
|
->select('model')
|
|
->where('is_active', 1)
|
|
->where('make', $make)
|
|
->groupBy('model')
|
|
->orderBy('model')
|
|
->limit($limit);
|
|
|
|
if ($q) {
|
|
$builder->like('model', $q);
|
|
}
|
|
|
|
return array_column($builder->get()->getResultArray(), 'model');
|
|
}
|
|
|
|
public function variants(string $make, string $model, int $limit = 200): array
|
|
{
|
|
return $this->builder()
|
|
->select('vehicle_code, variant, body_type, fuel_type, seating_capacity, cubic_capacity, production_status')
|
|
->where('is_active', 1)
|
|
->where('make', $make)
|
|
->where('model', $model)
|
|
->orderBy('variant')
|
|
->limit($limit)
|
|
->get()
|
|
->getResultArray();
|
|
}
|
|
}
|