2026-01-07 08:59:36 +07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Database\Seeds;
|
|
|
|
|
|
|
|
|
|
use CodeIgniter\Database\Seeder;
|
2026-01-07 16:55:25 +07:00
|
|
|
use CodeIgniter\HTTP\CURLRequest;
|
2026-01-07 08:59:36 +07:00
|
|
|
|
|
|
|
|
class AreaGeoSeeder extends Seeder
|
|
|
|
|
{
|
|
|
|
|
/**
|
2026-01-07 16:55:25 +07:00
|
|
|
* API configuration for fetching zones data
|
2026-01-07 08:59:36 +07:00
|
|
|
*/
|
2026-01-07 16:55:25 +07:00
|
|
|
protected string $apiUrl = 'https://your-api-domain.com/api/zones';
|
2026-01-07 08:59:36 +07:00
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
|
{
|
2026-01-07 16:55:25 +07:00
|
|
|
// Fetch data from external API
|
|
|
|
|
$options = [
|
|
|
|
|
'baseURI' => $this->apiUrl,
|
|
|
|
|
'timeout' => 30,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$client = new CURLRequest($options);
|
|
|
|
|
|
|
|
|
|
$response = $client->get('');
|
|
|
|
|
|
|
|
|
|
if ($response->getStatusCode() !== 200) {
|
|
|
|
|
echo "Failed to fetch data from API. Status: " . $response->getStatusCode() . "\n";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$externalData = $response->getJSON(true);
|
2026-01-07 08:59:36 +07:00
|
|
|
|
|
|
|
|
if (empty($externalData)) {
|
2026-01-07 16:55:25 +07:00
|
|
|
echo "No data found from API.\n";
|
2026-01-07 08:59:36 +07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Prepare data for insertion (exclude AreaGeoID to allow auto-increment)
|
|
|
|
|
$data = [];
|
|
|
|
|
foreach ($externalData as $row) {
|
|
|
|
|
$data[] = [
|
2026-01-07 16:55:25 +07:00
|
|
|
'AreaCode' => $row['zonecode'] ?? null,
|
|
|
|
|
'Class' => $row['zoneclass'] ?? null,
|
|
|
|
|
'AreaName' => str_replace('_', ' ', $row['zonename'] ?? ''),
|
|
|
|
|
'Parent' => $row['parentzoneid'] ?? null,
|
2026-01-07 08:59:36 +07:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Insert into local database
|
|
|
|
|
$this->db->table('areageo')->insertBatch($data);
|
|
|
|
|
|
|
|
|
|
echo "Successfully seeded " . count($data) . " area geo records.\n";
|
|
|
|
|
}
|
|
|
|
|
}
|