migrate_projekt_juurdepaas.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. use Drupal\Core\Database\Database;
  3. use Drupal\node\Entity\Node;
  4. $src = Database::getConnection('default', 'migrate');
  5. $dst = Database::getConnection();
  6. // Migreeri field_projekt_juurdepaas (projekt -> kasutajad).
  7. // D7 tabel: field_data_field_projekt_juurdepaas (entity_id=projekt nid, target_id=user uid)
  8. // D11 tabel: node__field_projekt_juurdepaas
  9. $rows = $src->query(
  10. "SELECT entity_id, field_projekt_juurdepaas_target_id, delta
  11. FROM {field_data_field_projekt_juurdepaas}"
  12. );
  13. $count = 0;
  14. $by_node = [];
  15. foreach ($rows as $r) {
  16. $nid = (int) $r->entity_id;
  17. $uid = (int) $r->field_projekt_juurdepaas_target_id;
  18. // Kontrolli, et node ja kasutaja eksisteerivad D11-s.
  19. $node_exists = $dst->query('SELECT COUNT(*) FROM {node} WHERE nid = :nid', [':nid' => $nid])->fetchField();
  20. $user_exists = $dst->query('SELECT COUNT(*) FROM {users_field_data} WHERE uid = :uid', [':uid' => $uid])->fetchField();
  21. if (!$node_exists || !$user_exists) {
  22. echo "SKIP nid=$nid uid=$uid (puudub)\n";
  23. continue;
  24. }
  25. $by_node[$nid][] = $uid;
  26. }
  27. foreach ($by_node as $nid => $uids) {
  28. $node = Node::load($nid);
  29. if (!$node) {
  30. echo "SKIP NODE $nid\n";
  31. continue;
  32. }
  33. $node->set('field_projekt_juurdepaas', array_map(fn($u) => ['target_id' => $u], $uids));
  34. $node->save();
  35. $count++;
  36. echo "SET nid=$nid users=" . implode(',', $uids) . "\n";
  37. }
  38. echo "done, nodes updated: $count\n";