Phalcon Model Data insert turns duplicate

Below Code inserting City data twice. when i check for last insert id city data will duplicate is it a bug???

<?php

namespace Shopping\Controllers;

use Shopping\Models\Globcities; use Shopping\Models\Globpincodes;

class IndexController extends ControllerBase {

public function indexAction()
{
    include $this->config->application->libraryDir . 'simple_html_dom.php';
    $url = 'http://www.mapsofindia.com/pincode/india/karnataka/';


    $x = 'bangalore';
    $x = explode('|', $x);
    foreach ($x as $a) {
        $a = trim(preg_replace('%/$%', '', $a));
        $city = new Globcities();
        $city->city_name = $a;
        $city->city_state_id = 17;
        if ($city->create()) {
            //City Last Insert ID
            $city_id = $city->city_id;
            $html = file_get_html($url . $a);
            $pincodes = array();
            foreach ($html->find('.table_hide tr') as $k => $element) {
                if ($k == 0) continue;
                $Pin = (int)(trim($element->children(1)->plaintext));
                $pincodes[$Pin] = $Pin;
            }
            foreach ($pincodes as $p) {
                $pincode = new Globpincodes();
                $pincode->pincode_city_id = $city_id;
                $pincode->pincode_number = $p;
                $pincode->create();
            }
        }
     }
}

}



44.9k
edited Dec '14

First off use ```php with a newline above it to start the block to get syntax highlighting.

Check that your aren't having a side effect with the browser calling the phalcon/server action twice. Then try logging an error message every time that the loop is run before the loop and inside of it. Error log when your index or bootstrap file is run to check for the browser side effect (double Ajax/Post call).