Skip to content

Commit 68ce6a3

Browse files
committed
Update UserManager.php
1 parent da2c4a8 commit 68ce6a3

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

Manager/UserManager.php

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,22 +1541,43 @@ public function getUserSearchableFields()
15411541
*/
15421542
public function bindUserToOrganization()
15431543
{
1544+
$limit = 2000;
1545+
$offset = 0;
1546+
$this->log('Add organizations to users...');
15441547
$this->objectManager->startFlushSuite();
1545-
$users = $this->getAll();
1548+
$countUsers = $this->objectManager->count('ClarolineCoreBundle:User');
15461549
$default = $this->organizationManager->getDefault();
15471550
$i = 0;
1551+
$detach = [];
15481552

1549-
foreach ($users as $user) {
1550-
if (count($user->getOrganizations()) === 0) {
1551-
$i++;
1552-
$this->log('Add default organization for user ' . $user->getUsername());
1553-
$user->addOrganization($default);
1554-
$this->objectManager->persist($user);
1553+
while ($offset < $countUsers) {
1554+
$users = $this->userRepo->findBy(array(), null, $limit, $offset);
15551555

1556-
if ($i % self::MAX_EDIT_BATCH_SIZE == 0) {
1557-
$this->objectManager->forceFlush();
1556+
foreach ($users as $user) {
1557+
if (count($user->getOrganizations()) === 0) {
1558+
++$i;
1559+
$this->log('Add default organization for user '.$user->getUsername());
1560+
$user->addOrganization($default);
1561+
$this->objectManager->persist($user);
1562+
$detach[] = $user;
1563+
1564+
if ($i % 250 == 0) {
1565+
$this->log("Flushing... [UOW = {$this->objectManager->getUnitOfWork()->size()}]");
1566+
$this->objectManager->forceFlush();
1567+
1568+
foreach ($detach as $el) {
1569+
$this->objectManager->detach($el);
1570+
}
1571+
1572+
$detach = [];
1573+
}
1574+
} else {
1575+
$this->log("Organization for user {$user->getUsername()} already exists");
1576+
$this->objectManager->detach($user);
15581577
}
15591578
}
1579+
1580+
$offset += $limit;
15601581
}
15611582

15621583
$this->objectManager->endFlushSuite();

0 commit comments

Comments
 (0)