Skip to content

Commit 1692ce6

Browse files
fix: Fix performance when refreshing news
Whenever we inserted a link in the news, we synchronised the publication frequency. This had a critical impact on performance. Commit introducing the issue: c189583
1 parent e6bfa63 commit 1692ce6

File tree

2 files changed

+45
-22
lines changed

2 files changed

+45
-22
lines changed

src/models/Journal.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ public function fill(int $max): int
4545
$link->save();
4646

4747
// And don't forget to add the link to the news collection!
48-
$link->addCollection($news, at: $news_link->published_at);
48+
$link->addCollection(
49+
$news,
50+
at: $news_link->published_at,
51+
sync_publication_frequency: false,
52+
);
4953
}
5054

5155
Link::groupLinksBySources($news->id);

src/models/Link.php

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,19 @@ public function collections(): array
202202
*
203203
* @param Collection[] $collections
204204
*/
205-
public function setCollections(array $collections, ?\DateTimeImmutable $at = null): void
206-
{
205+
public function setCollections(
206+
array $collections,
207+
?\DateTimeImmutable $at = null,
208+
bool $sync_publication_frequency = true,
209+
): void {
207210
$collection_ids = array_column($collections, 'id');
208211
LinkToCollection::setCollections($this->id, $collection_ids, $at);
209212

210-
foreach ($collections as $collection) {
211-
$collection->syncPublicationFrequencyPerYear();
212-
$collection->save();
213+
if ($sync_publication_frequency) {
214+
foreach ($collections as $collection) {
215+
$collection->syncPublicationFrequencyPerYear();
216+
$collection->save();
217+
}
213218
}
214219
}
215220

@@ -218,47 +223,61 @@ public function setCollections(array $collections, ?\DateTimeImmutable $at = nul
218223
*
219224
* @param Collection[] $collections
220225
*/
221-
public function addCollections(array $collections, ?\DateTimeImmutable $at = null): void
222-
{
226+
public function addCollections(
227+
array $collections,
228+
?\DateTimeImmutable $at = null,
229+
bool $sync_publication_frequency = true,
230+
): void {
223231
$collection_ids = array_column($collections, 'id');
224232
LinkToCollection::attach([$this->id], $collection_ids, $at);
225233

226-
foreach ($collections as $collection) {
227-
$collection->syncPublicationFrequencyPerYear();
228-
$collection->save();
234+
if ($sync_publication_frequency) {
235+
foreach ($collections as $collection) {
236+
$collection->syncPublicationFrequencyPerYear();
237+
$collection->save();
238+
}
229239
}
230240
}
231241

232242
/**
233243
* Add the link to a collection.
234244
*/
235-
public function addCollection(Collection $collection, ?\DateTimeImmutable $at = null): void
236-
{
237-
$this->addCollections([$collection], $at);
245+
public function addCollection(
246+
Collection $collection,
247+
?\DateTimeImmutable $at = null,
248+
bool $sync_publication_frequency = true,
249+
): void {
250+
$this->addCollections([$collection], $at, $sync_publication_frequency);
238251
}
239252

240253
/**
241254
* Remove the link from the collections.
242255
*
243256
* @param Collection[] $collections
244257
*/
245-
public function removeCollections(array $collections): void
246-
{
258+
public function removeCollections(
259+
array $collections,
260+
bool $sync_publication_frequency = true,
261+
): void {
247262
$collection_ids = array_column($collections, 'id');
248263
LinkToCollection::detach([$this->id], $collection_ids);
249264

250-
foreach ($collections as $collection) {
251-
$collection->syncPublicationFrequencyPerYear();
252-
$collection->save();
265+
if ($sync_publication_frequency) {
266+
foreach ($collections as $collection) {
267+
$collection->syncPublicationFrequencyPerYear();
268+
$collection->save();
269+
}
253270
}
254271
}
255272

256273
/**
257274
* Remove the link from a collection.
258275
*/
259-
public function removeCollection(Collection $collection): void
260-
{
261-
$this->removeCollections([$collection]);
276+
public function removeCollection(
277+
Collection $collection,
278+
bool $sync_publication_frequency = true,
279+
): void {
280+
$this->removeCollections([$collection], $sync_publication_frequency);
262281
}
263282

264283
/**

0 commit comments

Comments
 (0)