Skip to content
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

Feature: Update Campaign Donation Query #7630

Merged

Conversation

alaca
Copy link
Member

@alaca alaca commented Nov 21, 2024

Description

This PR touches several places to update the CampaignDonationQuery class so that we are no longer using the give_campaign_forms lookup table to get donations. Since we don't have a donations table, the only fairly reasonable option was to add another meta field that will reference the campaign ID. The donation model is updated so that it is now possible to get a campaign from a donation object ($donation->campaign->id). The donation repository has also been updated to store the campaign ID as a donation meta. Also, Revenue DonationHandler is updated to insert the campaign ID on each donation, although I'm not entirely sure we need the campaign ID column in this table. It might be handy for reports.

Affects

Donation model
Donations repository
Donation meta
Give container
Revenue Donation Handler

Pre-review Checklist

  • Relevant @unreleased tags included in DocBlocks
  • Self Review of code and UX completed

Comment on lines 72 to 77
DB::table('give_donationmeta')
->insert([
'donation_id' => $donation->ID,
'meta_key' => DonationMetaKeys::CAMPAIGN_ID,
'meta_value' => $campaignId,
]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like this could be accomplished as a single insert of many rows?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, with SQL. With WPDB, not really. I would love to have that feature. Maybe we should consider updating the insert method to support that.

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like a shame to knowingly have a nasty N+1 issue here that we could definitely circumvent just because of a lack of a WPDB feature. I think it's worth taking the moment to build the insert many feature for this.

Copy link
Member Author

@alaca alaca Nov 26, 2024

Choose a reason for hiding this comment

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

@JasonTheAdams, I have been thinking about this in the past. Since data for inserting will probably be collected inside the loop, I thought having something like this might be handy:

        $qb = DB::table('give_donationmeta');

        foreach($data as $row) {
            $qb->set([
                'donation_id' => $row->id,
                'meta_key' => $row->key,
                'meta_value' => $row->value,
            ]);
        }

        $qb->insert();

This approach will require altering the insert method to be something like this:

    public function insert($data = [], $format = null)
    {
        // collection represents collected data for insert
        if ($this->collection instanceof Collection) {
            return DB::query($this->getInsertIntoSQL());
        }
        
        if (empty($data)) {
            throw new InvalidArgumentException('data cannot be empty');
        }
        
        return DB::insert(
            $this->getTable(),
            $data,
            $format
        );
    }

@alaca
Copy link
Member Author

alaca commented Nov 26, 2024

@JasonTheAdams @kjohnson @jonwaldstein I added two new methods (set, and insertMany) in QB to support inserting many rows at once.
The set method extracts the column names and collects the data that will be inserted.
The insertMany method is used to run the query.

Example https://github.com/impress-org/givewp/pull/7630/files#diff-d8a0d0bca549b7c6ef03bff4d585bad86d5640245557cd38e16803554d6f4561R71

@@ -22,6 +23,12 @@ trait CRUD
*/
public function insert($data, $format = null)
{
if (array_is_list($data)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

@alaca Do we have a polyfill for this somewhere? This is a PHP 8.1 function.

Copy link
Member Author

Choose a reason for hiding this comment

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

@JasonTheAdams Yeah. I installed and am using the stellarwp/arrays package now.

…-donation-query-GIVE-1344

# Conflicts:
#	src/Campaigns/Repositories/CampaignRepository.php
@alaca alaca merged commit 1804935 into epic/campaigns Dec 4, 2024
20 checks passed
@alaca alaca deleted the feature/update-campaign-donation-query-GIVE-1344 branch December 4, 2024 08:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants