eee
This commit is contained in:
+30
-49
@@ -21,33 +21,15 @@ use JetBrains\PhpStorm\Pure;
|
||||
class Collection extends AbstractCollection
|
||||
{
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getItems(): array
|
||||
{
|
||||
// TODO: Change the autogenerated stub
|
||||
return $this->_item;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $field
|
||||
* @param string $field
|
||||
*
|
||||
* @return array|null
|
||||
* @throws
|
||||
*/
|
||||
public function values($field): ?array
|
||||
public function values(string $field): ?array
|
||||
{
|
||||
if (empty($field) || !is_string($field)) {
|
||||
return NULL;
|
||||
}
|
||||
$_tmp = [];
|
||||
$data = $this->toArray();
|
||||
foreach ($data as $val) {
|
||||
/** @var ModelInterface $val */
|
||||
$_tmp[] = $val[$field];
|
||||
}
|
||||
return $_tmp;
|
||||
return array_values($this->column($field));
|
||||
}
|
||||
|
||||
|
||||
@@ -58,15 +40,10 @@ class Collection extends AbstractCollection
|
||||
*/
|
||||
public function update(array $attributes): bool
|
||||
{
|
||||
$lists = [];
|
||||
$model = $this->getModel();
|
||||
if (!$this->isEmpty()) {
|
||||
foreach ($this->_item as $item) {
|
||||
$lists[] = $item[$model->getPrimary()];
|
||||
}
|
||||
return $model::query()->whereIn($model->getPrimary(), $lists)->update($attributes);
|
||||
if ($this->isEmpty()) {
|
||||
return $this->getLogger()->failure('No data by update', 'mysql');
|
||||
}
|
||||
return false;
|
||||
return $this->batch()->update($attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,18 +57,9 @@ class Collection extends AbstractCollection
|
||||
foreach ($column as $key => $value) {
|
||||
$column[$key] = $array[$value];
|
||||
}
|
||||
|
||||
return $column;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function orderRand(): static
|
||||
{
|
||||
shuffle($this->_item);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $start
|
||||
@@ -101,13 +69,13 @@ class Collection extends AbstractCollection
|
||||
*/
|
||||
#[Pure] public function slice(int $start = 0, int $length = 20): array
|
||||
{
|
||||
if (empty($this->_item)) {
|
||||
if (empty($this->getItems())) {
|
||||
return [];
|
||||
}
|
||||
if (\count($this->_item) < $length) {
|
||||
return $this->_item;
|
||||
if (\count($this->getItems()) < $length) {
|
||||
return $this->getItems();
|
||||
} else {
|
||||
return array_slice($this->_item, $start, $length);
|
||||
return array_slice($this->getItems(), $start, $length);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,7 +117,7 @@ class Collection extends AbstractCollection
|
||||
*/
|
||||
#[Pure] public function current(): ModelInterface|array
|
||||
{
|
||||
return current($this->_item);
|
||||
return current($this->getItems());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -157,7 +125,7 @@ class Collection extends AbstractCollection
|
||||
*/
|
||||
#[Pure] public function size(): int
|
||||
{
|
||||
return count($this->_item);
|
||||
return count($this->getItems());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -171,7 +139,6 @@ class Collection extends AbstractCollection
|
||||
foreach ($this as $value) {
|
||||
$array[] = $value->toArray();
|
||||
}
|
||||
$this->_item = [];
|
||||
return $array;
|
||||
}
|
||||
|
||||
@@ -182,10 +149,24 @@ class Collection extends AbstractCollection
|
||||
public function delete(): bool
|
||||
{
|
||||
$model = $this->getModel();
|
||||
if ($model->hasPrimary()) {
|
||||
return $model::query()->whereIn($model->getPrimary(), $this->column($model->getPrimary()))->delete();
|
||||
if ($this->isEmpty()) {
|
||||
return $this->getLogger()->failure('No data by delete', 'mysql');
|
||||
}
|
||||
throw new Exception('Must set primary key. if you wante delete');
|
||||
if (!$model->hasPrimary()) {
|
||||
throw new Exception('Must set primary key. if you want to delete data');
|
||||
}
|
||||
return $this->batch()->delete();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return ActiveQuery
|
||||
* @throws Exception
|
||||
*/
|
||||
private function batch(): ActiveQuery
|
||||
{
|
||||
return $this->makeNewQuery()->whereIn($this->getModel()->getPrimary(),
|
||||
$this->column($this->getModel()->getPrimary()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -205,7 +186,7 @@ class Collection extends AbstractCollection
|
||||
}
|
||||
$_filters[] = $value;
|
||||
}
|
||||
return new Collection($this->query, $_filters, $this->model);
|
||||
return new Collection($this->query, $this->model, $_filters);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user