Skip to content

Save custom data in writeRecord() before civi.dao.postInsert/postUpdate - #33005

Closed
mattwire wants to merge 1 commit into
civicrm:masterfrom
mattwire:daosavecustom
Closed

mattwire wants to merge 1 commit into
civicrm:masterfrom
mattwire:daosavecustom

Conversation

@mattwire

@mattwire mattwire commented Jun 17, 2025 •

Copy link
Copy Markdown
Contributor

Overview

civi.dao.postInsert/civi.dao.postUpdate (and hook_civicrm_postSave_<table>) fire before writeRecord() saves custom data, so listeners see missing or stale custom values. See https://lab.civicrm.org/extensions/civirules/-/issues/244.

Before

On create, a listener reading the custom table finds nothing; on update it finds the previous value.

After

Custom data is stored immediately after the insert/update, before those events are dispatched.

Technical Details

  • writeRecord() hands $record['custom'] to the instance; save() stores it after insert()/update() and clears it.
  • hook_civicrm_custom now fires before the DAO post events rather than after.
  • Only affects entities saved through writeRecord(); BAOs with their own create() (e.g. Contact, Contribution) are unchanged.
  • Test added: CRM_Core_DAOTest::testWriteRecordSavesCustomDataBeforePostEvents.

Comments

Rewritten from the earlier proof of concept: the copyValues() change is dropped. Exposing custom values in API4 form on the DAO events (via EntityParamsTrait, per #35492) can follow separately - related https://lab.civicrm.org/dev/core/-/work_items/5413

@civibot

civibot Bot commented Jun 17, 2025

Copy link
Copy Markdown

🤖 Thank you for contributing to CiviCRM! ❤️ We will need to test and review this PR. 👷

Introduction for new contributors...
  • If this is your first PR, an admin will greenlight automated testing with the command ok to test or add to whitelist.
  • A series of tests will automatically run. You can see the results at the bottom of this page (if there are any problems, it will include a link to see what went wrong).
  • A demo site will be built where anyone can try out a version of CiviCRM that includes your changes.
  • If this process needs to be repeated, an admin will issue the command test this please to rerun tests and build a new demo site.
  • Before this PR can be merged, it needs to be reviewed. Please keep in mind that reviewers are volunteers, and their response time can vary from a few hours to a few weeks depending on their availability and their knowledge of this particular part of CiviCRM.
  • A great way to speed up this process is to "trade reviews" with someone - find an open PR that you feel able to review, and leave a comment like "I'm reviewing this now, could you please review mine?" (include a link to yours). You don't have to wait for a response to get started (and you don't have to stop at one!) the more you review, the faster this process goes for everyone 😄
  • To ensure that you are credited properly in the final release notes, please add yourself to contributor-key.yml
  • For more information about contributing, see CONTRIBUTING.md.
Quick links for reviewers...

➡️ Online demo of this PR 🔗

@civibot civibot Bot added the master label Jun 17, 2025
Comment thread CRM/Core/DAO.php Resolved
Comment thread CRM/Core/DAO.php Resolved
Comment thread CRM/Core/DAO.php Resolved
@mattwire
mattwire force-pushed the daosavecustom branch 2 times, most recently from b99ee8b to 36a0eac Compare June 18, 2025 11:24
@mattwire

Copy link
Copy Markdown
Contributor Author

@MegaphoneJon You might be interested in this (it would fix https://lab.civicrm.org/extensions/civirules/-/issues/244)

@MegaphoneJon

Copy link
Copy Markdown
Contributor

I tested this today and I like it generally but it didn't work with copied records. I opened #35339 and #35340 to handle that.

@colemanw

colemanw commented Apr 7, 2026

Copy link
Copy Markdown
Member

This seems related to https://lab.civicrm.org/dev/core/-/work_items/5413 which has been sponsored by WMF so I'll be working on it shortly. Will circle back to see how this one relates.

Comment thread CRM/Core/DAO.php Outdated
Comment on lines +1001 to +1009
// In order to return (and eventually save) using API4 style customFields
// We need to copy them to the DAO object.
// @fixme: Need to load spec and parse through foreach above
$api4CustomFields = array_filter($params, function($key) {
return str_contains($key, '.');
}, ARRAY_FILTER_USE_KEY);
foreach ($api4CustomFields as $fieldName => $fieldValue) {
$this->$fieldName = $fieldValue;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mattwire what does this code do? I'm guessing that for Api4 calls it copies the custom fields to the object, but for non-api4 calls it does nothing?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes exactly. It's identifying API4 style custom fields by the "." ie. "CustomGroup.CustomFieldname". and copying them onto the DAO object so they are passed through to the post hook and returned to the user. Because otherwise they get filtered out even if you pass them in.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea ok, but maybe we don't need this if we add getParam/setParam helpers, since this only works for Api4 but the helpers will work regardless of how the hook is called.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'm experimenting by creating a copy of getValues() here and using that. I don't think we actually need getValue()/setValue() for this PR?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mattwire FYI the above-mentioned getter/setter methods were adde here: #35492

@colemanw

Copy link
Copy Markdown
Member

I would suggest adding methods similar to these to the PreUpdate and PostUpdate objects for making the custom array workable using api4 syntax.

@mattwire
mattwire force-pushed the daosavecustom branch 3 times, most recently from 7fe47b7 to 2cae74c Compare June 3, 2026 11:06
@mattwire mattwire changed the title Proof of concept: Save custom data before emiting postX events Save custom data in writeRecord() before civi.dao.postInsert/postUpdate Oct 2, 2026
@mattwire
mattwire marked this pull request as ready for review October 2, 2026 08:32
@mattwire

mattwire commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

@colemanw I've reworked this. What do you think of it now?

@colemanw colemanw added the merge ready PR will be merged after a few days if there are no objections label Oct 2, 2026
Comment thread CRM/Core/DAO.php

if (!empty($record['custom']) && is_array($record['custom'])) {
CRM_Core_BAO_CustomValueTable::store($record['custom'], static::getTableName(), $instance->$idField, $op);
$instance->_customParams = $record['custom'];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a very fine nuance of PHP. You're accessing a private instance variable from a static method. Normally that's not allowed, but PHP allows it here because the method is the same class as the instance. So in PHP, private seems to mean "scoped to only this class" not "only this instance".
The PHP docs are not super clear on this point, but it works and tests pass :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah.. I'm not totally liking the private class variable but it does seem the simplest way of doing what we need.

@aydun

aydun commented Oct 31, 2026

Copy link
Copy Markdown
Contributor

Minor: tests now avoid hard-coded location type ids (seeded ids are offset in tests). Please use $this->getLocationTypeID('Home'), or 'location_type_id:name' => 'Home' in APIv4. It passes today only because nothing checks the id.

@mattwire

mattwire commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

Minor: tests now avoid hard-coded location type ids (seeded ids are offset in tests). Please use $this->getLocationTypeID('Home'), or 'location_type_id:name' => 'Home' in APIv4. It passes today only because nothing checks the id.

Thanks @aydun - switched to 'location_type_id:name' => 'Home'.

@mattwire

mattwire commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

/rebase

writeRecord() stored custom data after save(), so listeners on civi.dao.postInsert, civi.dao.postUpdate and hook_civicrm_postSave_<table> read missing or stale custom values. save() now stores the custom data immediately after the insert/update, before those events are dispatched.
@colemanw

colemanw commented Nov 6, 2026

Copy link
Copy Markdown
Member

@mattwire this is what the completed vision would look like: #37943

@mattwire

Copy link
Copy Markdown
Contributor Author

Closing in favour of #37943

@colemanw colemanw removed the merge ready PR will be merged after a few days if there are no objections label Nov 26, 2026
@colemanw

Copy link
Copy Markdown
Member

@mattwire I think you pushed to this branch and it reopened the PR. Closing again.

@colemanw colemanw closed this Nov 26, 2026
@mattwire

Copy link
Copy Markdown
Contributor Author

I might have just forgotten to actually click close!

@mattwire
mattwire deleted the daosavecustom branch November 26, 2026 17:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants