We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Best way to delete the value of a custom field for 100s or 1000s of entries at once?

Development and Programming

Brian Litzinger's avatar
Brian Litzinger
693 posts
6 years ago
Brian Litzinger's avatar Brian Litzinger

This is related to my other question in that it is for a new fieldtype. I have a scenario where I want to delete the value of a field for more than 1 entry at a time. I’m assuming using the entry model and calling ->save() will be very intensive for a large batch of entries, so this is what I came up with (EE5+ only). Is there a better way using the models or some other service I’m not seeing?

/*
fields example:

[
field_id_1 => [1,2,3...],
field_id_2 => [4,5,6...]
]
*/
$entryResult = new EntryResult();
        $entries = $entryResult->getEntries();
        $entryIds = $entries->pluck('entry_id');
        $fields = array_keys($entryResult->getFieldsToChannelIds());

        if (!empty($entryIds) && !empty($fields)) {
            foreach ($fields as $field) {
                $tableName = App::getFieldTableName($field); // e.g. exp_channel_data_field_X
                ee('db')->where_in('entry_id', $entryIds)->delete($tableName);
            }
        }
       
Robin Sowell's avatar
Robin Sowell
13,158 posts
6 years ago
Robin Sowell's avatar Robin Sowell

It’s definitely a problem. You’re right, saving that many entries isn’t going to scale well. But, we rely on the assumption that a change in the Entry data will trigger the on ‘Save events on the Model’.

Is it a use case where you can batch and still use save?

And on a purely pragmatic level, I think the above might run into an issue with legacy fields in exp_channel_data.

       
Brian Litzinger's avatar
Brian Litzinger
693 posts
6 years ago
Brian Litzinger's avatar Brian Litzinger

What would a batch update with the models look like?

I was only going to make this EE5 and above, and since it’s a new FT the data should never be saved into the old exp_channel_data table.

       
Brian Litzinger's avatar
Brian Litzinger
693 posts
6 years ago
Brian Litzinger's avatar Brian Litzinger

I updated it to use the checkboxes and only update 20 entries at a time.

       
Robin Sowell's avatar
Robin Sowell
13,158 posts
6 years ago
Robin Sowell's avatar Robin Sowell

Argh- sorry, Brian. I missed your reply.

> I was only going to make this EE5 and above,

Ah- that makes sense.

> What would a batch update with the models look like?

I was thinking something ajaxy like the file sync or the database exporter and using save. Which I’m not sure is what I would actually do, but it would allow save to be used.

> I updated it to use the checkboxes and only update 20 entries at a time.

Which I think is in the same vein.

I pinged Seth, as he’s way better with the models, and said you could try

$things = ee('Model')->get('ChannelEntry', [2,22,435,23,25])->all();
$things->set(['field_id_27' => 'puppy']);
$things->save();

->set() and ->save() should work across the Collection, doing the action to each element. So that might be an alternative if you need one.

       
Brian Litzinger's avatar
Brian Litzinger
693 posts
6 years ago
Brian Litzinger's avatar Brian Litzinger

That makes a lot of sense. Thanks for sending that example. I tried it, but it did something that I don’t think is necessary for what I’m trying to do. Since it’s calling save on the model, it triggered the after_channel_entry_save() hook in other add-ons. For example in this case, I don’t think it is a good idea for Publisher to do it’s whole saving routine just to nullify a field that other add-ons won’t care about.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.