newRecord = true; } public function save() { try { $pdo = MysqlPdo::get(); if ($this->developer_name) { $developerId = $this->findOrAddDeveloper($pdo, $this->developer_name); } else { $developerId = null; } $nbObjectId = $this->findOrAddObject($pdo, $this->object_name); if ($this->housing_name) { $nbHousingId = $this->findOrAddHousing($pdo, $this->housing_name); } else { $nbHousingId = null; } $regionRfId = $this->findRegion($pdo, $this->regionKLADRcode); if ($this->newRecord) { $sql = "INSERT INTO `newbuildings`( `xml_id`, `name`, `newbuilding_object_id`, `newbuilding_housing_id`, `developer_id`, `cian_id`, `cian_korpus_id`, `yandex_id`, `emls_id`, `avito_object_id`, `avito_housing_id`, `rf_region_id` ) VALUES( :xml_id, :name, :newbuilding_object_id, :newbuilding_housing_id, :developer_id, :cian_id, :cian_korpus_id, :yandex_id, :emls_id, :avito_object_id, :avito_housing_id, :rf_region_id )"; $pdo->queryNamed($sql, array( ':xml_id' => $this->id, ':name' => $this->name, ':newbuilding_object_id' => $nbObjectId, ':newbuilding_housing_id' => $nbHousingId, ':developer_id' => $developerId, ':cian_id' => $this->cian_id, ':cian_korpus_id' => $this->cian_korpus_id, ':yandex_id' => $this->yandex_id, ':emls_id' => $this->emls_id, ':avito_object_id' => $this->avito_object_id, ':avito_housing_id' => $this->avito_housing_id, ':rf_region_id' => $regionRfId, )); } else { $sql = "UPDATE `newbuildings` SET `name` = :name, `newbuilding_object_id` = :newbuilding_object_id, `newbuilding_housing_id` = :newbuilding_housing_id, `developer_id` = :developer_id, `updated_at` = NOW(), `cian_id` = :cian_id, `cian_korpus_id` = :cian_korpus_id, `yandex_id` = :yandex_id, `emls_id` = :emls_id, `avito_object_id` = :avito_object_id, `avito_housing_id` = :avito_housing_id, `rf_region_id` = :rf_region_id WHERE xml_id = :xml_id"; $pdo->queryNamed($sql, array( ':xml_id' => $this->id, ':name' => $this->name, ':newbuilding_object_id' => $nbObjectId, ':newbuilding_housing_id' => $nbHousingId, ':developer_id' => $developerId, ':cian_id' => $this->cian_id, ':cian_korpus_id' => $this->cian_korpus_id, ':yandex_id' => $this->yandex_id, ':emls_id' => $this->emls_id, ':avito_object_id' => $this->avito_object_id, ':avito_housing_id' => $this->avito_housing_id, ':rf_region_id' => $regionRfId, )); } return true; } catch (Exception $e) { echo $e->getMessage(); return false; } } private function findOrAddDeveloper(MysqlPdo $pdo, $developerName) { $sql = "SELECT id FROM developer WHERE value = :value"; $rez = $pdo->queryNamed($sql, array(':value' => $developerName)); if ($pdo->num_rows($rez) > 0) { return $pdo->fetch_assoc($rez)['id']; } else { $sql = "INSERT INTO developer (value) VALUES (:value)"; $pdo->queryNamed($sql, array(':value' => $developerName)); return $pdo->insert_id(); } } private function findOrAddObject(MysqlPdo $pdo, $objectName) { $sql = "SELECT id FROM newbuilding_object WHERE value = :value"; $rez = $pdo->queryNamed($sql, array(':value' => $objectName)); if ($pdo->num_rows($rez) > 0) { return $pdo->fetch_assoc($rez)['id']; } else { $sql = "INSERT INTO newbuilding_object (value) VALUES (:value)"; $pdo->queryNamed($sql, array(':value' => $objectName)); return $pdo->insert_id(); } } private function findOrAddHousing(MysqlPdo $pdo, $housingName) { $sql = "SELECT id FROM newbuilding_housing WHERE value = :value"; $rez = $pdo->queryNamed($sql, array(':value' => $housingName)); if ($pdo->num_rows($rez) > 0) { return $pdo->fetch_assoc($rez)['id']; } else { $sql = "INSERT INTO newbuilding_housing (value) VALUES (:value)"; $pdo->queryNamed($sql, array(':value' => $housingName)); return $pdo->insert_id(); } } private function findRegion(MysqlPdo $pdo, $kladrCode) { $sql = "SELECT id FROM rf_regions WHERE kladr = :value"; $rez = $pdo->queryNamed($sql, array(':value' => $kladrCode)); if ($pdo->num_rows($rez) > 0) { return $pdo->fetch_assoc($rez)['id']; } else { return null; } } public function getIsNewRecord() { return $this->newRecord; } public static function deleteAll($condition = '') { throw new Exception("deleteALL not implemented"); } public static function className() { return get_called_class(); } public static function findOne($id) { $pdo = MysqlPdo::get(); $sql = "SELECT xml_id, name FROM newbuildings WHERE xml_id='$id'"; $rez = $pdo->query($sql); if ($pdo->num_rows($rez)) { $item = new ZipalNewbuildingItemNew(); $item->newRecord = false; $dbObject = $pdo->fetch_assoc($rez); $item->id = $dbObject['xml_id']; $item->name = $dbObject['name']; return $item; } return null; } }