How can I insert many field values under one term? I'm working on a module, of which one function is grab list names and stock codes from a custom table and create corresponding terms. Now, my current code creates a new term and corresponding stock code, however this is not very efficient. How can I create one term for each unique list name and create corresponding many field values? Below is my current code:

function liste() {
    $src = ['Ý', 'Þ', 'ý', 'þ', 'ð'];
    $tar = ['İ', 'Ş', 'ı', 'ş', 'ğ'];
    $query = \Drupal::database()-&gt;select("excel","m")-&gt;fields("m",["stokkodu","grupadi"])-&gt;condition("grupadi", "","!=")-&gt;execute()-&gt;fetchAll();
    foreach ($query as $q) {
        $term = Term::create([
            'vid' =&gt; 'liste_adi',
            'name' =&gt; str_replace($src, $tar, $q-&gt;grupadi),
            'field_stok_kodu' =&gt; $q-&gt;stokkodu,
        ]);
        $term-&gt;enforceIsNew();
        $term-&gt;save();
    }
}


I thought about selecting only grupadi and grouping by it and inserting to a temporary table and then inserting corresponding stock codes in another column delimited with comma or something; but if there is another more efficient way, please let me know. EDIT: I'm looking to do something like the following:

Current:

| Manyetik Matkaplar?n Yedek Par�alar?                                                | YMD2299001      |
| Manyetik Matkaplar?n Yedek Par�alar?                                                | YMD2299002      |
| Manyetik Matkaplar?n Yedek Par�alar?                                                | YMD2299003      |
| Manyetik Matkaplar?n Yedek Par�alar?                                                | YMD2299004      |
| Manyetik Matkaplar?n Yedek Par�alar?                                                | YMD2299005      |
| Manyetik Matkaplar?n Yedek Par�alar?                                                | YMD2303001      |
| Manyetik Matkaplar?n Yedek Par�alar?                                                | YMD2303002      |
| Manyetik Matkaplar?n Yedek Par�alar?                                                | YMD2303003      |
| Manyetik Matkaplar?n Yedek Par�alar?                                                | YMD2303004      |
| Manyetik Matkaplar?n Yedek Par�alar?                                                | YMD2303017      |
| Manyetik Matkaplar?n Yedek Par�alar?                                                | YMD2303022      |
| Manyetik Matkaplar?n Yedek Par�alar?                                                | YMD2303046      |
| Manyetik Matkaplar?n Yedek Par�alar?                                                | YMD2601044      |
| Manyetik Matkaplar?n Yedek Par�alar?                                                | YMD2608020      |
+-------------------------------------------------------------------------------------+-----------------+


Desired:

| Manyetik Matkaplar?n Yedek Par�alar?                                                | YMD2299001,YMD2299002,YMD2299003


I hope you get the idea.
You needn't have to do aggregation via temporary tables, it can be done in your query via a GROUP_CONCAT expression &amp; GROUP clause:

$query = \Drupal::database()-&gt;select("excel","m")-&gt;fields("m",["stokkodu","grupadi"])-&gt;condition("grupadi", "","!=");
$query-&gt;addExpression('GROUP_CONCAT(m.stokkodu)', 'stokkodus');
$query-&gt;groupBy('m.grupadi');
$results = $query-&gt;execute()-&gt;fetchAll(); 
foreach ($results as $result) {
  $result-&gt;stokkodus contains a comma-delimited list of values that you can split with explode()
  $stokkodus = explode(',');
  // @TODO: add transform logic
}


In the example above, you can transform the exploded array then create an array of values for your multivalue field_stok_kodu field:

  // @TODO: add transform logic
  'field_stok_kodu' =&gt; [
    ['value' =&gt; $stokkodus[0]],
    ['value' =&gt; $stokkodus[1]]
  ];