Get Select field options from database

I have a model that fetches an array from a database:

class VariationCustomPage extends Kirby\Cms\Page {
  public function getMaterials() {
    $pdo = connectDB();

    $query = 'SELECT id, el material_el, en material_en FROM dbo.prdalloys';

    $statement = $pdo->query($query);
    $materials = $statement->fetchAll(PDO::FETCH_ASSOC);

    return array_map(function($material) {
      return (object) $material;
    }, $materials);

This produces a result of this type:

    [0] => stdClass Object
            [id] => 1
            [material_el] => Χρυσός Κ14
            [material_en] => Gold Κ14

    [1] => stdClass Object
            [id] => 2
            [material_el] => Χρυσός Κ18
            [material_en] => Gold Κ18


The blueprint field that fetches the options:

    label: Material
    type: select
    options: query
      fetch: page.getMaterials
      text: "{{ arrayItem.material_el }}"
      value: "{{ arrayItem.material_el }}"

The problem is that text and value are not parsed, they are displayed literally as “{{ arrayItem.material_el }}” in the select dropdown.

The reason this doesn’t work is because you have an array of objects, not an array of arrays and therefore no arrayItem. Replace arrayItem with item.

Thanks, that worked. I returned an array of arrays at first, but I was getting a “get_class() expects parameter 1 to be object, array given” error on the blueprint field, so I ended up using an array of objects.