Files
pathfinder/app/main/model/universe/structuremodel.php
Mark Friedrich eb52a0754d - New "Intel module" for Citadel tracking, closed #246
- added some new tables (for SDE replacement), #628
- new "index build" functions added to `/setup`, #628
- updated "Cortex" PHP ORM lib `1.5.0` -> `1.6.0-dev`
2018-05-31 22:51:06 +02:00

123 lines
3.2 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: exodu
* Date: 14.10.2017
* Time: 15:56
*/
namespace Model\Universe;
use DB\SQL;
use DB\SQL\Schema;
use lib\Util;
class StructureModel extends BasicUniverseModel {
protected $table = 'structure';
protected $fieldConf = [
'name' => [
'type' => Schema::DT_VARCHAR128,
'nullable' => false,
'default' => ''
],
'systemId' => [
'type' => Schema::DT_INT,
'nullable' => false,
'default' => 0,
'index' => true
],
'typeId' => [
'type' => Schema::DT_INT,
'index' => true,
'belongs-to-one' => 'Model\Universe\TypeModel',
'constraint' => [
[
'table' => 'type',
'on-delete' => 'CASCADE'
]
],
'validate' => 'validate_notDry'
],
'x' => [
'type' => Schema::DT_FLOAT,
'nullable' => false,
'default' => 0
],
'y' => [
'type' => Schema::DT_FLOAT,
'nullable' => false,
'default' => 0
],
'z' => [
'type' => Schema::DT_FLOAT,
'nullable' => false,
'default' => 0
]
];
/**
* get data from object
* -> more fields can be added in here if needed
* @return \stdClass
*/
public function getData(): \stdClass {
$data = (object) [];
if(!$this->dry()){
$data->id = $this->_id;
$data->name = $this->name;
}
return $data;
}
/**
* load data from API into $this and save $this
* @param int $id
* @param string $accessToken
* @param array $additionalOptions
*/
protected function loadData(int $id, string $accessToken = '', array $additionalOptions = []){
$data = self::getF3()->ccpClient->getUniverseStructureData($id, $accessToken, $additionalOptions);
if(!empty($data)){
$type = $this->rel('typeId');
$type->loadById($data['typeId'], $accessToken, $additionalOptions);
$data['typeId'] = $type;
$this->copyfrom($data);
$this->save();
}
}
/**
* @param array|string $key
* @param null $fields
* @return NULL
*/
public function copyfrom($key, $fields = null){
// flatten array (e.g. "position" key)
$key = Util::arrayFlattenByKey((array)$key);
parent::copyfrom($key, $fields);
}
/**
* overwrites parent
* @param null|SQL $db
* @param null $table
* @param null $fields
* @return bool
*/
public static function setup($db=null, $table=null, $fields=null){
if($status = parent::setup($db,$table,$fields)){
//change `id` column to BigInt
$schema = new Schema($db);
$typeQuery = $schema->findQuery($schema->dataTypes[Schema::DT_BIGINT]);
$db->exec("ALTER TABLE " . $db->quotekey('structure') .
" MODIFY COLUMN " . $db->quotekey('id') . " " . $typeQuery . " NOT NULL");
}
return $status;
}
}