| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- <?php
- use Drupal\Core\Database\Database;
- use Drupal\node\Entity\Node;
- $src = Database::getConnection('default', 'migrate');
- $dst = Database::getConnection();
- // Migreeri field_projekt_juurdepaas (projekt -> kasutajad).
- // D7 tabel: field_data_field_projekt_juurdepaas (entity_id=projekt nid, target_id=user uid)
- // D11 tabel: node__field_projekt_juurdepaas
- $rows = $src->query(
- "SELECT entity_id, field_projekt_juurdepaas_target_id, delta
- FROM {field_data_field_projekt_juurdepaas}"
- );
- $count = 0;
- $by_node = [];
- foreach ($rows as $r) {
- $nid = (int) $r->entity_id;
- $uid = (int) $r->field_projekt_juurdepaas_target_id;
- // Kontrolli, et node ja kasutaja eksisteerivad D11-s.
- $node_exists = $dst->query('SELECT COUNT(*) FROM {node} WHERE nid = :nid', [':nid' => $nid])->fetchField();
- $user_exists = $dst->query('SELECT COUNT(*) FROM {users_field_data} WHERE uid = :uid', [':uid' => $uid])->fetchField();
- if (!$node_exists || !$user_exists) {
- echo "SKIP nid=$nid uid=$uid (puudub)\n";
- continue;
- }
- $by_node[$nid][] = $uid;
- }
- foreach ($by_node as $nid => $uids) {
- $node = Node::load($nid);
- if (!$node) {
- echo "SKIP NODE $nid\n";
- continue;
- }
- $node->set('field_projekt_juurdepaas', array_map(fn($u) => ['target_id' => $u], $uids));
- $node->save();
- $count++;
- echo "SET nid=$nid users=" . implode(',', $uids) . "\n";
- }
- echo "done, nodes updated: $count\n";
|