You can use entry queries to fetch the entries and the elements service to save them. You can access the element service using Craft::$app->elements
. Use Craft::$app->elements->saveElement($entry);
to save an entry (or any other element).
The best place to put business logic is a service class that you can then call from your controllers. So something like this:
use Craft;
use craft\elements\Entry;
public function updateEntriesWithCustomInfo()
{
/** @var craft\services\Elements $elementService */
$elementService = Craft::$app->elements;
// add conditions here
$entries = Entry::find()->limit(1500)->all();
foreach ($entries as $entry) {
// update your field
$entry->setFieldValue('my_custom_field', 'some_value');
$success = $elementService->saveElement($entry);
if (!$success) {
// saving failed, log error or abort
}
}
}
Keep in mind that this might take a while if you have to process 1500 entries at once. If this isn't a one-off, it would be better to put this in a custom queue job. Then your service method would just find the entries to update and add queue jobs with their IDs to the queue. The queue job can either process one element at a time or a batch of elements (e.g. in batches of 25), depending on your requirements.