Server IP : 180.180.241.3 / Your IP : 216.73.216.63 Web Server : Microsoft-IIS/7.5 System : Windows NT NETWORK-NHRC 6.1 build 7601 (Windows Server 2008 R2 Standard Edition Service Pack 1) i586 User : IUSR ( 0) PHP Version : 5.3.28 Disable Function : NONE MySQL : ON | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : C:/AppServ/www/app/Config/ |
Upload File : |
<?php App::uses( 'ClassRegistry', 'Utility' ); class GeographyCsvDataImporter { public function __construct() { $model = ClassRegistry::init( 'Region' ); $sql = file_get_contents( dirname( __FILE__ ) . '/rpd-queries.txt' ); $model->query( $sql ); } } /* class GeographyCsvDataImporter { private $__Region; private $__Province; private $__District; private function __insertRegion( $name ) { $region = $this->__Region->findByName( $name ); if( !empty( $region ) ) { return $region['Region']['id']; } $this->__Region->create(); $this->__Region->save( array( 'name' => $name, ) ); return $this->__Region->getInsertID(); } private function __insertProvince( $name, $idRegion ) { $data = array( 'name' => $name, 'region_id' => $idRegion, ); $province = $this->__Province->find( 'first', array( 'conditions' => $data ) ); if( !empty( $province ) ) { return $province['Province']['id']; } $this->__Province->create(); $this->__Province->save( $data ); return $this->__Province->getInsertID(); } private function __insertDistrict( $name, $idProvince ) { $data = array( 'name' => $name, 'province_id' => $idProvince, ); $district = $this->__District->find( 'first', array( 'conditions' => $data ) ); if( !empty( $district ) ) { return $district['District']['id']; } $this->__District->create(); $this->__District->save( $data ); return $this->__District->getInsertID(); } public function __construct( $filepath ) { // initialize our models $this->__Region = ClassRegistry::init( 'Region' ); $this->__Province = ClassRegistry::init( 'Province' ); $this->__District = ClassRegistry::init( 'District' ); $this->__Region->recursive = $this->__Province->recursive = $this->__District->recursive = -1; // read the csv file and import its content $fh = fopen( $filepath, 'r' ); if( $fh !== false ) { while( $line = fgetcsv( $fh , 1024 ) ) { $idRegion = $this->__insertRegion( $line[2] ); $idProvince = $this->__insertProvince( $line[1], $idRegion ); $idDistrict = $this->__insertDistrict( $line[0], $idProvince ); } fclose( $fh ); } // We have one province with the region_id = 7 (`Northern`) // We delete this region and update the province with region_id = 5 $province = $this->__Province->find( 'first', array( 'conditions' => array( 'region_id' => 7, ), ) ); // update the province $province['Province']['region_id'] = 5; $this->__Province->save( $province['Province'] ); // delete region; we cannot call delete() because it is loogin for a `is_activated` field $db = ConnectionManager::getDataSource('default'); $db->rawQuery( 'DELETE FROM regions WHERE id = 7' ); } } // GeographyCsvDataImporter */