improve country handling

This commit is contained in:
Dennis Eichhorn 2021-02-06 13:47:12 +01:00
parent acedec0db1
commit 22cd674189
6 changed files with 78 additions and 3 deletions

View File

@ -249,6 +249,14 @@ class DataMapperAbstract implements DataMapperInterface
*/
protected static string $datetimeFormat = 'Y-m-d H:i:s';
/**
* Raw query data from last query
*
* @var array
* @since 1.0.0
*/
protected static array $lastQueryData = [];
/**
* Constructor.
*
@ -1449,6 +1457,11 @@ class DataMapperAbstract implements DataMapperInterface
*/
private static function updateModel(object $obj, mixed $objId, \ReflectionClass $refClass = null, int $depth = 1) : void
{
// Model doesn't have anything to update
if (\count(static::$columns) < 2) {
return;
}
$query = new Builder(self::$db);
$query->update(static::$table)
->where(static::$table . '.' . static::$primaryField, '=', $objId);
@ -1507,9 +1520,14 @@ class DataMapperAbstract implements DataMapperInterface
}
}
$sth = self::$db->con->prepare($query->toSql());
if ($sth !== false) {
$sth->execute();
try {
$sth = self::$db->con->prepare($query->toSql());
if ($sth !== false) {
$sth->execute();
}
} catch (\Throwable $t) {
echo $t->getMessage();
echo $query->toSql();
}
}
@ -2641,6 +2659,11 @@ class DataMapperAbstract implements DataMapperInterface
if (!empty($keys) || $primaryKey === null) {
$dbData = self::getRaw($keys, self::$relations, $depth, $ref, $query);
if (static::class === self::$parentMapper) {
static::$lastQueryData = $dbData;
}
foreach ($dbData as $row) {
$value = $row[static::$primaryField . '_' . $depth];
$obj[$value] = self::createBaseModel();
@ -2664,6 +2687,11 @@ class DataMapperAbstract implements DataMapperInterface
return $obj;
}
public static function getDataLastQuery() : array
{
return static::$lastQueryData;
}
/**
* Get object.
*

View File

@ -84,6 +84,10 @@ abstract class GrammarAbstract
'COUNT(',
'MAX(',
'MIN(',
'SUM(',
'DATE(',
'YEAR(',
'MONTH(',
];
/**

View File

@ -1402,6 +1402,7 @@ class Builder extends BuilderAbstract
public function execute() : mixed
{
try {
$t = $this->toSql();
$sth = $this->connection->con->prepare($this->toSql());
foreach ($this->binds as $key => $bind) {

View File

@ -72,6 +72,22 @@ class Country
*/
protected string $subdevision = '';
/**
* Country region.
*
* @var string
* @since 1.0.0
*/
protected string $region = '';
/**
* Country developed.
*
* @var string
* @since 1.0.0
*/
protected bool $isDeveloped = false;
/**
* Get id
*
@ -143,4 +159,28 @@ class Country
{
return $this->subdevision;
}
/**
* Get country region
*
* @return string
*
* @since 1.0.0
*/
public function getRegion() : string
{
return $this->region;
}
/**
* Is country developed
*
* @return bool
*
* @since 1.0.0
*/
public function isDeveloped() : bool
{
return $this->isDeveloped;
}
}

View File

@ -38,6 +38,8 @@ class CountryMapper extends DataMapperAbstract
'country_code2' => ['name' => 'country_code2', 'type' => 'string', 'internal' => 'code2'],
'country_code3' => ['name' => 'country_code3', 'type' => 'string', 'internal' => 'code3'],
'country_numeric' => ['name' => 'country_numeric', 'type' => 'int', 'internal' => 'numeric'],
'country_region' => ['name' => 'country_region', 'type' => 'string', 'internal' => 'region'],
'country_developed' => ['name' => 'country_developed', 'type' => 'bool', 'internal' => 'isDeveloped'],
];
/**