Joywork/import/obj/BlockSubway.php
2026-05-22 21:21:54 +03:00

145 lines
4.4 KiB
PHP

<?php
/**
* This is the model class for table "block_subways".
*
* @property integer $id
* @property integer $block_id
* @property integer $subway_id
* @property string $distance
* @property string $created_at
* @property string $updated_at
* @property string $newRecord
*/
class BlockSubway implements ImportRecord
{
public $id;
public $block_id;
public $subway_id;
public $distance;
public $created_at;
public $updated_at;
public $newRecord;
/**
* BlockSubway constructor.
*/
public function __construct()
{
$this->newRecord = true;
}
public function save()
{
try {
if ($this->newRecord) {
if (!isset($this->id)) {
$sql = "INSERT INTO block_subways (distance, subway_id, block_id, created_at) VALUES ('" . $this->distance .
"', '" . $this->subway_id . "', '" . $this->block_id . "', NOW())";
} else {
$sql = "INSERT INTO block_subways (id, distance, subway_id, block_id, created_at) VALUES ('" . $this->id .
"', '" . $this->distance . "', '" . $this->subway_id . "', '" . $this->block_id . "', NOW())";
}
mysql_query($sql);
$err = mysql_errno();
if ($err > 0) {
echo $err;
return false;
}
if (!isset($this->id)) {
$this->id = mysql_insert_id();
}
} else {
$sql = "UPDATE block_subways SET distance = '" . $this->distance . "', subway_id = '" . $this->subway_id .
"', block_id = '" . $this->block_id . "', updated_at = NOW() WHERE id = '" . $this->id . "'";
mysql_query($sql);
$err = mysql_errno();
if ($err > 0) {
echo $err;
return false;
}
}
return true;
} catch (Exception $e) {
echo $e->getMessage();
return false;
}
}
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)
{
$sql = "SELECT * FROM block_subways WHERE id='$id'";
$rez = mysql_query($sql);
if (mysql_num_rows($rez)) {
$blockSubway = new BlockSubway();
$blockSubway->newRecord = false;
$dbObject = mysql_fetch_array($rez);
$blockSubway->id = $dbObject['id'];
$blockSubway->distance = $dbObject['distance'];
$blockSubway->block_id = $dbObject['block_id'];
$blockSubway->subway_id = $dbObject['subway_id'];
$blockSubway->created_at = $dbObject['created_at'];
$blockSubway->updated_at = $dbObject['updated_at'];
return $blockSubway;
}
return null;
}
public static function findByBlockAndSubway($blockId, $subwayId)
{
$sql = "SELECT * FROM block_subways WHERE block_id='$blockId' and subway_id = '$subwayId'";
$rez = mysql_query($sql);
if (mysql_num_rows($rez)) {
$blockSubway = new BlockSubway();
$blockSubway->newRecord = false;
$dbObject = mysql_fetch_array($rez);
$blockSubway->id = $dbObject['id'];
$blockSubway->distance = $dbObject['distance'];
$blockSubway->block_id = $dbObject['block_id'];
$blockSubway->subway_id = $dbObject['subway_id'];
$blockSubway->created_at = $dbObject['created_at'];
$blockSubway->updated_at = $dbObject['updated_at'];
return $blockSubway;
}
return null;
}
public static function createStringsForBlock($blockId)
{
$sql = "SELECT s.name, bs.distance FROM block_subways bs, subways s WHERE s.id = bs.subway_id AND bs.block_id='$blockId'";
$rez = mysql_query($sql);
$strings = array();
if (mysql_num_rows($rez)) {
while($dbObject = mysql_fetch_assoc($rez)) {
$strings[] = $dbObject['name'] . '&nbsp;<span class="time">' . $dbObject['distance'] . '</span>';
}
}
return $strings;
}
}