Skip to content

Commit

Permalink
废弃模型查询时动态指定字段名的序列化字段处理
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Oct 23, 2023
1 parent 9c142be commit 8fff8b0
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 239 deletions.
2 changes: 2 additions & 0 deletions doc/base/version/2.1-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@
* 废弃 `Imi\Model\ModelManager`

* 废弃 `Model::updateBatch()``Model::deleteBatch()`

* 废弃模型查询时动态指定字段名的特殊处理
40 changes: 7 additions & 33 deletions src/Components/pgsql/tests/Unit/Model/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,40 +274,14 @@ public function testDbQueryAlias(): void

public function testQueryAlias(): void
{
/** @var Member $member */
$member = Member::query(null, null, null, 'a1')
->field('a1.username')
$result = Member::query(null, null, null, 'a1')
->where('a1.id', '=', 1)
->select()
->get();
$this->assertEquals([
'username' => '1',
], $member->toArray());
}

public function testQuerySetField(): void
{
/** @var Member $member */
$member = Member::query()->field('username')->where('id', '=', 1)->select()->get();
->find();
$this->assertInstanceOf(Member::class, $result);
$this->assertEquals([
'username' => '1',
], $member->toArray());

$member = Member::newInstance(['username' => 'test']);
$member->password = 'password';
$member->insert();
$id = $member->id;
$this->assertEquals([
'id' => $id,
'username' => 'test',
], $member->toArray());

$member = Member::find($id);
$this->assertEquals([
'id' => $id,
'username' => 'test',
], $member->toArray());
$this->assertEquals('password', $member->password);
'id' => 1,
'username' => '1',
], $result->toArray());
}

/**
Expand Down Expand Up @@ -537,7 +511,7 @@ public function testNotCamel(): void
'json_data' => [4, 5, 6],
]], TestJson::convertListToArray($list));

$record = TestJsonNotCamel::query()->field('id', 'json_data')->where('id', '=', $id)->select()->get();
$record = TestJsonNotCamel::query()->where('id', '=', $id)->select()->get();
$this->assertEquals([
'id' => $id,
'json_data' => [4, 5, 6],
Expand Down
65 changes: 2 additions & 63 deletions src/Model/ModelQueryResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
*/
class ModelQueryResult extends Result
{
/**
* 是否设置模型序列化字段.
*/
protected bool $isSetSerializedFields = false;

/**
* 关联查询预加载字段.
*/
Expand Down Expand Up @@ -71,14 +66,7 @@ public function get(?string $className = null)
}
else
{
if ($this->isSetSerializedFields)
{
$serializedFields = $this->parseFieldNames($meta->getSerializableFieldNames(), array_keys($record));
}
else
{
$serializedFields = $meta->getParsedSerializableFieldNames();
}
$serializedFields = $meta->getParsedSerializableFieldNames();
foreach ($with as $k => $v)
{
if (\is_string($k))
Expand Down Expand Up @@ -107,10 +95,6 @@ public function get(?string $className = null)
{
$object->__setSerializedFields($withField);
}
elseif ($this->isSetSerializedFields)
{
$object->__setSerializedFields($this->parseFieldNames($meta->getSerializableFieldNames(), array_keys($record)));
}
}
if ($meta->isBean())
{
Expand Down Expand Up @@ -176,14 +160,7 @@ public function getArray(?string $className = null): array
}
else
{
if ($this->isSetSerializedFields)
{
$serializedFields = $this->parseFieldNames($meta->getSerializableFieldNames(), array_keys($statementRecords[0]));
}
else
{
$serializedFields = $meta->getParsedSerializableFieldNames();
}
$serializedFields = $meta->getParsedSerializableFieldNames();
foreach ($with as $k => $v)
{
if (\is_string($k))
Expand All @@ -201,10 +178,6 @@ public function getArray(?string $className = null): array
{
$serializedFields = $withField;
}
elseif ($this->isSetSerializedFields)
{
$serializedFields = $this->parseFieldNames($meta->getSerializableFieldNames(), array_keys($statementRecords[0]));
}
else
{
$serializedFields = [];
Expand Down Expand Up @@ -252,26 +225,6 @@ public function getArray(?string $className = null): array
return $list;
}

/**
* Get 是否设置模型序列化字段.
*/
public function getIsSetSerializedFields(): bool
{
return $this->isSetSerializedFields;
}

/**
* Set 是否设置模型序列化字段.
*
* @param bool $isSetSerializedFields 是否设置模型序列化字段
*/
public function setIsSetSerializedFields(bool $isSetSerializedFields): self
{
$this->isSetSerializedFields = $isSetSerializedFields;

return $this;
}

/**
* Get 关联查询预加载字段.
*/
Expand Down Expand Up @@ -307,18 +260,4 @@ public function setWithField(?array $withField): self

return $this;
}

private function parseFieldNames(array $serializableFieldNames, array $fieldNames): array
{
foreach ($fieldNames as &$name)
{
// @deprecated 3.0 将废弃动态字段名
if (!isset($serializableFieldNames[$name]) && false !== ($index = array_search($name, $serializableFieldNames)))
{
$name = $index;
}
}

return $fieldNames;
}
}
83 changes: 8 additions & 75 deletions src/Model/Traits/TModelQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Imi\Model\Traits;

use Imi\Db\Interfaces\IDb;
use Imi\Db\Query\Field;
use Imi\Db\Query\Interfaces\IResult;
use Imi\Db\Query\Result\ChunkByOffsetResult;
use Imi\Db\Query\Result\ChunkResult;
Expand All @@ -16,11 +15,6 @@

trait TModelQuery
{
/**
* 是否设置序列化字段.
*/
protected bool $isSetSerializedFields = false;

/**
* 关联查询预加载字段.
*/
Expand Down Expand Up @@ -67,25 +61,17 @@ protected function initQuery(): void

private function queryPreProcess(): void
{
if ($this->hasCustomFields())
{
$this->isSetSerializedFields = true;
}
else
/** @var \Imi\Model\Meta $meta */
$meta = $this->modelClass::__getMeta();
if ($sqlColumns = $meta->getSqlColumns())
{
/** @var \Imi\Model\Meta $meta */
$meta = $this->modelClass::__getMeta();
if ($sqlColumns = $meta->getSqlColumns())
$this->field($meta->getTableName() . '.*');
$fields = $meta->getFields();
foreach ($sqlColumns as $name => $sqlAnnotations)
{
$this->field($meta->getTableName() . '.*');
$fields = $meta->getFields();
foreach ($sqlColumns as $name => $sqlAnnotations)
{
$sqlAnnotation = $sqlAnnotations[0];
$this->fieldRaw($sqlAnnotation->sql, $fields[$name]->name ?? $name);
}
$sqlAnnotation = $sqlAnnotations[0];
$this->fieldRaw($sqlAnnotation->sql, $fields[$name]->name ?? $name);
}
$this->isSetSerializedFields = false;
}
}

Expand Down Expand Up @@ -139,66 +125,13 @@ public function chunkEach(int $count, string $column, ?string $alias = null)
return parent::chunkEach($count, $column, $alias);
}

private function hasCustomFields(): bool
{
$field = $this->option->field;
if (!$field)
{
return false;
}
if (\count($field) > 1)
{
return true;
}

$k = array_key_first($field);
$v = $field[$k] ?? null;

if (\is_int($k))
{
if ('*' === $v)
{
return false;
}
if ($v instanceof Field)
{
$field = $v;
}
else
{
$field = new Field();
$field->setValue($v ?? '', $this);
}
}
else
{
$field = new Field(null, null, $k, $v);
}
if ('*' !== $field->getField())
{
return true;
}
$table = $field->getTable();
$tableObject = $this->option->table;
if (null === $table || $table === $tableObject->getTable() || $table === $tableObject->getAlias())
{
return false;
}

return true;
}

/**
* 执行SQL语句.
*/
public function execute(string $sql): IResult
{
/** @var ModelQueryResult $result */
$result = parent::execute($sql);
if ($this->isSetSerializedFields)
{
$result->setIsSetSerializedFields(true);
}
if ($this->with)
{
$result->setWith($this->with);
Expand Down
71 changes: 3 additions & 68 deletions tests/unit/Component/Tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,64 +394,6 @@ public function testColumn(array $args): void
$this->assertEquals(array_column_ex($origin, ['username', 'id'], 'id'), $data);
}

/**
* @depends testInsert
*/
public function testQuerySetField(array $args): void
{
['id' => $id] = $args;
/** @var Member $member */
$member = Member::query()->field('username')->where('id', '=', $id)->select()->get();
$this->assertEquals([
'username' => '1',
], $member->toArray());

$member = Member::newInstance(['username' => 'test']);
$member->password = 'password';
$member->insert();
$id = $member->id;
$this->assertEquals([
'id' => $id,
'username' => 'test',
], $member->toArray());

$member = Member::find($id);
$this->assertEquals([
'id' => $id,
'username' => 'test',
], $member->toArray());
$this->assertEquals('password', $member->password);
}

/**
* @depends testInsert
*/
public function testFieldRaw(array $args): void
{
['id' => $id] = $args;

/** @var Member $record */
$record = Member::query()->fieldRaw('*, 123 as notInJson, 456 as tmpField')->where('id', '=', $id)->select()->get();
$this->assertEquals([
'id' => $id,
'username' => '1',
'password' => 'pw2',
'notInJson' => 123,
'tmpField' => 456,
], $record->convertToArray());

$list = Member::query()->fieldRaw('*, 123 as notInJson, 456 as tmpField')->where('id', '=', $id)->select()->getArray();
$this->assertEquals([
[
'id' => $id,
'username' => '1',
'password' => 'pw2',
'notInJson' => 123,
'tmpField' => 456,
],
], Member::convertListToArray($list));
}

/**
* @param CreateTime|UpdateTime $record
*/
Expand Down Expand Up @@ -748,7 +690,7 @@ public function testNotCamel(): void
'json_data' => [4, 5, 6],
]], TestJson::convertListToArray($list));

$record = TestJsonNotCamel::query()->field('id', 'json_data')->where('id', '=', $id)->select()->get();
$record = TestJsonNotCamel::query()->where('id', '=', $id)->select()->get();
$this->assertEquals([
'id' => $id,
'json_data' => [4, 5, 6],
Expand Down Expand Up @@ -820,12 +762,12 @@ public function testCustomFields(): void
$memberArray['id2'] = $memberArray['id'];

/** @var MemberReferenceProperty|null $member1 */
$member1 = MemberReferenceProperty::query()->field('tb_member.*')->where('id', '=', $member->id)->select()->get();
$member1 = MemberReferenceProperty::query()->where('id', '=', $member->id)->select()->get();
$this->assertNotNull($member1);
$this->assertEquals($memberArray, $member1->toArray());

/** @var MemberReferenceProperty|null $member1 */
$list = MemberReferenceProperty::query()->field('tb_member.*')->where('id', '=', $member->id)->select()->getArray();
$list = MemberReferenceProperty::query()->where('id', '=', $member->id)->select()->getArray();
$this->assertNotNull($member1);
$this->assertEquals([$memberArray], Member::convertListToArray($list));
}
Expand Down Expand Up @@ -1160,13 +1102,6 @@ public function testBug403(): void
$this->assertEquals([
'id' => $id,
], $record->convertToArray());

$record = TestBug403::query()->field('id', 'json_data')->where('id', '=', $id)->select()->get();
$this->assertEquals([
'id' => $id,
'json_data' => [4, 5, 6],
], $record->convertToArray());
$this->assertEquals([4, 5, 6], $record->getJsonData()->toArray());
}

public function testIncrUpdate(): void
Expand Down

0 comments on commit 8fff8b0

Please sign in to comment.