From 069196ecf790bfbba505b4ef0fba5e9ee29eb303 Mon Sep 17 00:00:00 2001 From: eileen Date: Tue, 2 Jul 2019 12:23:06 +1200 Subject: [PATCH] [wip] bulk create --- CRM/Core/BAO/CustomField.php | 19 +++++++++++++++++++ .../phpunit/CRM/Core/BAO/CustomFieldTest.php | 11 +++++++++++ 2 files changed, 30 insertions(+) diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index ebb578d947cf..52cecf579d43 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -193,6 +193,25 @@ protected static function doCreate($params) { return $customField; } + /** + * Create several fields at once in a mysql efficient way. + * + * The intention is that apiv4 would expose any BAO with bulkCreate as a new action. + * + * Note that in the first instance this supports 'create' only. It's possible edit + * is fine in the same function - just treading slowly. + * + * @param array $params + * Array of arrays as would be passed into create + * @param array $defaults + * Default parameters to be be merged into each of the params. + */ + public function bulkCreate($params, $defaults) { + foreach ($params as $fieldParams) { + $fieldParams = array_merge($defaults, $fieldParams); + } + } + /** * Fetch object based on array of properties. * diff --git a/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php b/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php index 70449f8d6ccb..19aa373f72e7 100644 --- a/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php +++ b/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php @@ -636,4 +636,15 @@ public function testGetFieldsForImport() { $this->assertEquals($expected, CRM_Core_BAO_CustomField::getFieldsForImport()); } + /** + * Test the bulk create function works. + */ + public function testBulkCreate() { + $customGroup = $this->customGroupCreate([ + 'extends' => 'Individual', + 'title' => 'my bulk group', + ]); + CRM_Core_BAO_CustomField::bulkCreate([]); + } + }