4
0

update 2023-06-20 03:05:20

This commit is contained in:
Robot 2023-06-20 03:05:20 +02:00
parent 6cff5ffe50
commit 5fa40eb556
Signed by: Robot
GPG Key ID: 14DECD44E7E1BB95
3 changed files with 64 additions and 18 deletions

View File

@ -16,8 +16,9 @@ class Load << (F,LightGreen) >> #Green {
# Database $load # Database $load
+ __construct(Table $table, Model $model, ...) + __construct(Table $table, Model $model, ...)
+ value(array $keys, string $field, ...) : mixed + value(array $keys, string $field, ...) : mixed
+ item(array $keys, ?string $table) : ?object + item(array $keys, string $table) : ?object
+ items(array $ids, string $table) : ?array + items(array $keys, string $table) : ?array
- prefix(array $keys) : array
} }
note right of Load::__construct note right of Load::__construct
@ -88,6 +89,13 @@ Example: $this->items($ids, 'table_name');
return: ?array return: ?array
end note end note
note right of Load::prefix
Add prefix to the keys
since: 2.0.1
return: array
end note
@enduml @enduml
``` ```

View File

@ -75,9 +75,9 @@ final class Load
* ], 'value_key', 'table_name' * ], 'value_key', 'table_name'
* ); * );
* *
* @param array $keys The item keys * @param array $keys The item keys
* @param string $field The field key * @param string $field The field key
* @param string $table The table * @param string $table The table
* *
* @return mixed * @return mixed
* @since 2.0.1 * @since 2.0.1
@ -112,13 +112,13 @@ final class Load
* ], 'table_name' * ], 'table_name'
* ); * );
* *
* @param array $keys The item keys * @param array $keys The item keys
* @param string $table The table * @param string $table The table
* *
* @return object|null * @return object|null
* @since 2.0.1 * @since 2.0.1
*/ */
public function item(array $keys, ?string $table): ?object public function item(array $keys, string $table): ?object
{ {
// check if this is a valid table // check if this is a valid table
if ($this->prefix($keys) && $this->table->exist($table)) if ($this->prefix($keys) && $this->table->exist($table))
@ -154,20 +154,20 @@ final class Load
* ); * );
* Example: $this->items($ids, 'table_name'); * Example: $this->items($ids, 'table_name');
* *
* @param array $ids The item ids * @param array $keys The item keys
* @param string $table The table * @param string $table The table
* *
* @return array|null * @return array|null
* @since 2.0.1 * @since 2.0.1
*/ */
public function items(array $ids, string $table): ?array public function items(array $keys, string $table): ?array
{ {
// check if this is a valid table // check if this is a valid table
if ($this->prefix($keys) && $this->table->exist($table)) if ($this->prefix($keys) && $this->table->exist($table))
{ {
return $this->model->items( return $this->model->items(
$this->load->items( $this->load->items(
['all' => 'a.*'], ['a' => $table], ['a.id' => $ids] ['all' => 'a.*'], ['a' => $table], $keys
), ),
$table $table
); );
@ -175,5 +175,24 @@ final class Load
return null; return null;
} }
/**
* Add prefix to the keys
*
* @param array $keys The query keys
*
* @return array
* @since 2.0.1
*/
private function prefix(array $keys): array
{
// update the key values
$bucket = [];
foreach ($keys as $k => $v)
{
$bucket['a.' . $k] = $v;
}
return $bucket;
}
} }

View File

@ -49,9 +49,9 @@
* ], 'value_key', 'table_name' * ], 'value_key', 'table_name'
* ); * );
* *
* @param array $keys The item keys * @param array $keys The item keys
* @param string $field The field key * @param string $field The field key
* @param string $table The table * @param string $table The table
* *
* @return mixed * @return mixed
* @since 2.0.1 * @since 2.0.1
@ -86,13 +86,13 @@
* ], 'table_name' * ], 'table_name'
* ); * );
* *
* @param array $keys The item keys * @param array $keys The item keys
* @param string $table The table * @param string $table The table
* *
* @return object|null * @return object|null
* @since 2.0.1 * @since 2.0.1
*/ */
public function item(array $keys, ?string $table): ?object public function item(array $keys, string $table): ?object
{ {
// check if this is a valid table // check if this is a valid table
if ($this->prefix($keys) && $this->table->exist($table)) if ($this->prefix($keys) && $this->table->exist($table))
@ -128,20 +128,20 @@
* ); * );
* Example: $this->items($ids, 'table_name'); * Example: $this->items($ids, 'table_name');
* *
* @param array $ids The item ids * @param array $keys The item keys
* @param string $table The table * @param string $table The table
* *
* @return array|null * @return array|null
* @since 2.0.1 * @since 2.0.1
*/ */
public function items(array $ids, string $table): ?array public function items(array $keys, string $table): ?array
{ {
// check if this is a valid table // check if this is a valid table
if ($this->prefix($keys) && $this->table->exist($table)) if ($this->prefix($keys) && $this->table->exist($table))
{ {
return $this->model->items( return $this->model->items(
$this->load->items( $this->load->items(
['all' => 'a.*'], ['a' => $table], ['a.id' => $ids] ['all' => 'a.*'], ['a' => $table], $keys
), ),
$table $table
); );
@ -149,3 +149,22 @@
return null; return null;
} }
/**
* Add prefix to the keys
*
* @param array $keys The query keys
*
* @return array
* @since 2.0.1
*/
private function prefix(array $keys): array
{
// update the key values
$bucket = [];
foreach ($keys as $k => $v)
{
$bucket['a.' . $k] = $v;
}
return $bucket;
}