229 lines
8.4 KiB
PHP
229 lines
8.4 KiB
PHP
<?php
|
|
|
|
/**
|
|
* This is the model class for table "zipal_newbuilding".
|
|
*
|
|
* @property string $id
|
|
* @property string $name
|
|
* @property string $object_name
|
|
* @property string $housing_name
|
|
* @property string $developer_name
|
|
* @property integer $cian_id
|
|
* @property integer $cian_korpus_id
|
|
* @property integer $yandex_id
|
|
* @property integer $emls_id
|
|
* @property integer $avito_object_id
|
|
* @property integer $avito_housing_id
|
|
* @property string $region
|
|
* @property string $regionKLADRcode
|
|
* @property boolean $newRecord
|
|
* @property string $created_at
|
|
* @property string $updated_at
|
|
*/
|
|
class ZipalNewbuildingItemNew implements ImportRecord
|
|
{
|
|
public $id;
|
|
public $name;
|
|
public $object_name;
|
|
public $housing_name;
|
|
public $developer_name;
|
|
public $cian_id;
|
|
public $cian_korpus_id;
|
|
public $yandex_id;
|
|
public $emls_id;
|
|
public $avito_object_id;
|
|
public $avito_housing_id;
|
|
public $region;
|
|
public $regionKLADRcode;
|
|
public $newRecord;
|
|
public $created_at;
|
|
public $updated_at;
|
|
|
|
/**
|
|
* Constructor.
|
|
*/
|
|
public function __construct() {
|
|
$this->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;
|
|
}
|
|
} |