-
-
Notifications
You must be signed in to change notification settings - Fork 825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[REF] Improve APIv4 save functions #22403
Conversation
(Standard links)
|
2065f6d
to
6c41af3
Compare
CRM/Campaign/BAO/Campaign.php
Outdated
@@ -65,11 +65,6 @@ public static function create(&$params) { | |||
} | |||
} | |||
|
|||
//store custom data |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'ved checked this one & confirm it
CRM/Campaign/BAO/Survey.php
Outdated
@@ -74,9 +74,6 @@ public static function create(&$params) { | |||
|
|||
$dao = self::writeRecord($params); | |||
|
|||
if (!empty($params['custom']) && is_array($params['custom'])) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'ved checked this one & confirm it
CRM/Core/BAO/WordReplacement.php
Outdated
* @param array $params | ||
* @param int $id | ||
* | ||
* @return array | ||
*/ | ||
public static function edit(&$params, &$id) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don' t think this function is ever called - we should go that step further & add a deprecation notice
We could go that step further & add a deprecation notice to the v3 function if it were to call something else itself - I think we could add in something like _civicrm_api3_basic_create
that would call apiv4 & return those results & start deprecating create functions where we can really migrate off them
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a longer-term goal I think.
@colemanw the last commit will mean that apiv3 updates custom twice in some cases - ie if it calls writeRecord on them & they are not one of the specified entities Would this last commit pass on it's own? Can it be it's own PR? Lines 1340 to 1344 in 2d35bc7
|
6c41af3
to
34eea26
Compare
@eileenmcnaughton of the 6 commits in this PR, the first 3 have been broken out & merged. I've broken the last one out to #22426 and included an APIv3 update in that PR. I've rebased this PR with the 2 remaining commits. |
@colemanw I actually pulled out the group contact stuff to this PR #22419 - however I hit something that has me scratching my head. I created the SubscriptionHistory api for the test so I decided I should use that instead of the BAO method in the post hook - but when I do it runs and works but doesn't commit to the db - this feels like something I've seen before in extensions implementing post hooks so now we have a test scenario it's failing in it feels worth figuring our rather than bypassing it (I think the general idea is that all crud should be by the v4 API - although classes do weird things like call add from create & such like) |
@@ -47,14 +47,16 @@ trait AddressSaveTrait { | |||
/** | |||
* @inheritDoc | |||
*/ | |||
protected function writeObjects(&$items) { | |||
foreach ($items as &$item) { | |||
protected function writeToDao(array $items) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to bike shed this function name a little - I found it really confusing and we are not actually writing to the dao as it implies. We are 'saving' or perhaps 'writeToDatabase'
If 'save' is contaminated? maybe 'saveRecords' ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. Just to frame the issue, what's going on in this PR is that one function is getting split into two smaller functions. The function is writeObjects()
which previously would:
- format the params
- call the appropriate BAO create/add/writeRecords function
- Reformat and return the results
Now it does 1 and 3, with 2 being split off to this new sub-function. For compatibility reasons I don't think the outer writeObjects
function should be renamed. So we need to pick a name for the inner sub-function. Something to indicate that it's called from writeObjects
and is the piece that takes care of the actual writing (or rather, handing off to the BAO to do the writing). So I named it writeToDao
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@colemanw what about writeToDatabase
- alternativelyperhap writeUsingDAO
- except it would really be writeUsingBAOExceptIfThereIsn'tOne
Or even just ... write
The writeToDAO
just doesn't seem to be what is actually being done - it's not being written to the DAO - and in some fantasy future world we could replace the DAO layer altogether.....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, write
sounds good.
b7c326a
to
49054d6
Compare
…ions Instead of using a hardcoded list of BAO functions to call, with extra hardcoded params to e.g. the Contact & Address BAOs, this switches to picking the BAO's create/add only if it's not `@deprecated`, and falling back to WriteRecords. Also refactors the large `writeObjects` function into two smaller functions so individual action classes can more easily override them.
49054d6
to
5a44345
Compare
@eileenmcnaughton ok this PR is now down to just one commit since you pulled the other one out into #22419 and I've renamed the function |
lol - looks OK to me - I'll go through a bit more carefully soon |
OK I took another look & this still looks OK (with the group contact one still needing resolution) - test cover is good. Merging |
Overview
Refactors APIv4 create/update functions to make the code more flexible & maintainable.
Before
Dealing with oddball BAOs meant a bunch of hardcoded exceptions written into the APIv4
writeObjects
function.After
APIv4
writeObjects
function refactored into smaller pieces.Oddball handling moved into specific APIs.
Use
@deprecated
annotations in the BAO to decide whether to use genericwriteRecords
function.Starts to centralize saving custom data rather than make each BAO responsible for it, which should make it easier to opt-in new entities to having custom data.