0

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()->select("excel","m")->fields("m",["stokkodu","grupadi"])->condition("grupadi", "","!=")->execute()->fetchAll();
    foreach ($query as $q) {
        $term = Term::create([
            'vid' => 'liste_adi',
            'name' => str_replace($src, $tar, $q->grupadi),
            'field_stok_kodu' => $q->stokkodu,
        ]);
        $term->enforceIsNew();
        $term->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.

2 Answers 2

0

You needn't have to do aggregation via temporary tables, it can be done in your query via a GROUP_CONCAT expression & GROUP clause:

$query = \Drupal::database()->select("excel","m")->fields("m",["stokkodu","grupadi"])->condition("grupadi", "","!=");
$query->addExpression('GROUP_CONCAT(m.stokkodu)', 'stokkodus');
$query->groupBy('m.grupadi');
$results = $query->execute()->fetchAll(); 
foreach ($results as $result) {
  $result->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' => [
    ['value' => $stokkodus[0]],
    ['value' => $stokkodus[1]]
  ];
0

I ended up doing the below. It might be overkill but does the job.

function liste() {
    $src = ['Ý', 'Þ', 'ý', 'þ', 'ð'];
    $tar = ['İ', 'Ş', 'ı', 'ş', 'ğ'];
    $grupadi = \Drupal::database()->select("excel","e")->fields("e",["grupadi"])->condition("grupadi", "","!=")->groupBy("grupadi")->execute()->fetchAll();
    foreach ($grupadi as $g) {
        $term = Term::create([
            'vid' => 'liste_adi',
            'name' => str_replace($src, $tar, $g->grupadi),
        ]);
        $term->enforceIsNew();
        $term->save();
    }
    $terms = \Drupal::entityTypeManager()->getStorage("taxonomy_term")->loadTree("liste_adi");
    foreach($terms as $term) {
        $stokkodu = \Drupal::database()->select("excel", "e")->fields("e", ["stokkodu"])->condition("grupadi", $term->name)->execute()->fetchAll();
        foreach($stokkodu as $s) {
            $t = Term::load($term->tid);
            $t->field_stok_kodu[] = $s->stokkodu;
            $t->save();
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.