From 74093233df9b1e879476461a5015ec5f64f43c46 Mon Sep 17 00:00:00 2001 From: ADmad Date: Wed, 13 Apr 2016 00:02:56 +0530 Subject: [PATCH] Add Table::saveMany(). This allows saving multiple records in a transaction. --- src/ORM/Table.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/ORM/Table.php b/src/ORM/Table.php index 79e6bda3323..a45d87080df 100644 --- a/src/ORM/Table.php +++ b/src/ORM/Table.php @@ -1618,6 +1618,32 @@ protected function _update($entity, $data) return $success; } + /** + * Saves multiple records for a table. + * + * @param array $entities Entities to save. + * @param array $options Options used when calling Table::save() for each entity. + * @return bool|array False on failure, entities list on succcess. + */ + public function saveMany($entities, $options = []) + { + $return = $this->connection()->transactional( + function () use ($entities, $options) { + foreach ($entities as $entity) { + if ($this->save($entity, $options) === false) { + return false; + } + } + } + ); + + if ($return === false) { + return false; + } + + return $entities; + } + /** * {@inheritDoc} *