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

129 lines
3.4 KiB
PHP

<?php
/**
* This is the model class for table "subways".
*
* @property integer $id
* @property string $name
* @property string $created_at
* @property string $updated_at
* @property boolean $newRecord
*/
class Subway implements ImportRecord
{
public $id;
public $name;
public $created_at;
public $updated_at;
public $newRecord;
/**
* Subway constructor.
*/
public function __construct()
{
$this->newRecord = true;
}
public function save()
{
try {
if ($this->newRecord) {
if (!isset($this->id)) {
$sql = "INSERT INTO subways (name, created_at) VALUES ('" . $this->name . "', NOW())";
} else {
$sql = "INSERT INTO subways (id, name, created_at) VALUES ('" . $this->id . "', '" . $this->name . "', NOW())";
}
mysql_query($sql);
$err = mysql_errno();
if ($err > 0) {
echo mysql_error();
return false;
}
if (!isset($this->id)) {
$this->id = mysql_insert_id();
}
} else {
$sql = "UPDATE subways SET name = '" . $this->name . "', updated_at = NOW() WHERE id = '" . $this->id . "'";
mysql_query($sql);
$err = mysql_errno();
if ($err > 0) {
echo mysql_error();
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 subways WHERE id='$id'";
$rez = mysql_query($sql);
if (mysql_num_rows($rez)) {
$subway = new Subway();
$subway->newRecord = false;
$dbObject = mysql_fetch_array($rez);
$subway->id = $dbObject['id'];
$subway->name = $dbObject['name'];
$subway->created_at = $dbObject['created_at'];
$subway->updated_at = $dbObject['updated_at'];
return $subway;
}
return null;
}
public static function getElementsList($temp_region)
{
$user = new User;
$user->get($_SESSION['id']);
if(isset($temp_region)) {
$region_sql = $temp_region;
} else {
$region_sql = $user->region_rf_id;
}
$sql = "SELECT * FROM subways WHERE rf_region_id = $region_sql ORDER BY name";
$rez = mysql_query($sql);
$subways = array();
if (mysql_num_rows($rez) > 0) {
while ($dbObject = mysql_fetch_assoc($rez)) {
$subway = new Subway();
$subway->newRecord = false;
$subway->id = $dbObject['id'];
$subway->name = $dbObject['name'];
$subway->created_at = $dbObject['created_at'];
$subway->updated_at = $dbObject['updated_at'];
$subways[] = $subway;
}
}
return $subways;
}
}