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);
}
}
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.
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.
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.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.