Skip to content

Commit 7d10766

Browse files
committed
Add updateBulk() and deleteBulk()
1 parent 57653e2 commit 7d10766

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

packages/orm/src/EntityMapper.php

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,24 @@ public function updateMultiple(
701701
return $results;
702702
}
703703

704+
/**
705+
* @param mixed $source The data we want to update to every rows.
706+
* @param Conditions $conditions Where conditions, you can use array or Compare object.
707+
* @param ORMOptions|int $options The options.
708+
*
709+
* @return StatementInterface
710+
* @throws \ReflectionException
711+
*
712+
* @deprecated Use updateBulk() instead
713+
*/
714+
public function updateWhere(
715+
array|object $source,
716+
mixed $conditions = null,
717+
ORMOptions|int $options = new ORMOptions()
718+
): StatementInterface {
719+
return $this->updateBulk($source, $conditions, $options);
720+
}
721+
704722
/**
705723
* Using one data to update multiple rows, filter by where conditions.
706724
* Example:
@@ -714,7 +732,7 @@ public function updateMultiple(
714732
* @return StatementInterface
715733
* @throws \ReflectionException
716734
*/
717-
public function updateWhere(
735+
public function updateBulk(
718736
array|object $source,
719737
mixed $conditions = null,
720738
ORMOptions|int $options = new ORMOptions()
@@ -994,14 +1012,47 @@ protected function prepareCreateInitData(mixed $initData, array $item, mixed $co
9941012
return $item;
9951013
}
9961014

1015+
public function deleteBulk(mixed $conditions, ORMOptions $options = new ORMOptions()): StatementInterface
1016+
{
1017+
if (is_object($conditions) && EntityMetadata::isEntity($conditions)) {
1018+
$conditions = Arr::only($this->extract($conditions), $this->getKeys());
1019+
}
1020+
1021+
$conditions = $this->conditionsToWheres($conditions);
1022+
1023+
// Event
1024+
1025+
$statement = $this->delete()
1026+
->where($conditions)
1027+
->execute();
1028+
1029+
// Event
1030+
1031+
return $statement;
1032+
}
1033+
9971034
/**
9981035
* @param Conditions $conditions
9991036
* @param ORMOptions|int $options
10001037
*
10011038
* @return void
10021039
* @throws \ReflectionException
1040+
*
1041+
* @deprecated Use deleteBatch() instead.
10031042
*/
10041043
public function deleteWhere(mixed $conditions, ORMOptions|int $options = new ORMOptions()): void
1044+
{
1045+
$this->deleteBatch($conditions, $options);
1046+
}
1047+
1048+
/**
1049+
* @param Conditions $conditions
1050+
* @param ORMOptions|int $options
1051+
*
1052+
* @return void
1053+
* @throws \ReflectionException
1054+
*/
1055+
public function deleteBatch(mixed $conditions, ORMOptions|int $options = new ORMOptions()): void
10051056
{
10061057
$options = clone ORMOptions::wrap($options);
10071058

packages/orm/src/ORMProxyTrait.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,23 @@ public function updateMultiple(
238238
return $this->mapper($entityClass)->updateMultiple($items, $condFields, $options);
239239
}
240240

241+
/**
242+
* @deprecated Use updateBulk() instead.
243+
*/
241244
public function updateWhere(
242245
string $entityClass,
243246
array|object $data,
244247
mixed $conditions = null,
245248
ORMOptions|int $options = new ORMOptions()
249+
): StatementInterface {
250+
return $this->updateBulk($entityClass, $data, $conditions, $options);
251+
}
252+
253+
public function updateBulk(
254+
string $entityClass,
255+
array|object $data,
256+
mixed $conditions = null,
257+
ORMOptions|int $options = new ORMOptions()
246258
): StatementInterface {
247259
return $this->mapper($entityClass)->updateWhere($data, $conditions, $options);
248260
}
@@ -341,12 +353,31 @@ public function updateOneOrCreate(
341353
return $this->mapper($entityClass)->updateOneOrCreate($item, $initData, $condFields, $options);
342354
}
343355

356+
public function deleteBulk(
357+
string $entityClass,
358+
mixed $conditions,
359+
ORMOptions|int $options = new ORMOptions()
360+
): StatementInterface {
361+
return $this->mapper($entityClass)->deleteBulk($conditions, $options);
362+
}
363+
364+
/**
365+
* @deprecated Use deleteBatch() instead.
366+
*/
344367
public function deleteWhere(
345368
string $entityClass,
346369
mixed $conditions,
347370
ORMOptions|int $options = new ORMOptions()
348371
): void {
349-
$this->mapper($entityClass)->deleteWhere($conditions, $options);
372+
$this->deleteBatch($entityClass, $conditions, $options);
373+
}
374+
375+
public function deleteBatch(
376+
string $entityClass,
377+
mixed $conditions,
378+
ORMOptions|int $options = new ORMOptions()
379+
): void {
380+
$this->mapper($entityClass)->deleteBatch($conditions, $options);
350381
}
351382

352383
/**

0 commit comments

Comments
 (0)