createChild structure

hi, i am folowing

how can i create structure field on the array?

class CommentsPage extends Kirby\Cms\Page
{
    public function children()
    {
        $comments = [];

        foreach (Db::select('AFILIADOS') as $comment) {
            $comments[] = [
                'slug'     => $comment->IDAFILIADO(),
                'num'      => 0,
                'template' => 'comment',
                'model'    => 'comment',
                'content'  => [
                    'nombre'  => $comment->NOMBRES(),
                    'apellido'  => $comment->APELLIDO(),
                    CREATE STRUCTURE FIELD????
                ]
            ];
        }

        return Pages::factory($comments, $this);
    }
}

Content for a structure field needs to be yaml encode.

You can use Data::encode($array, 'yaml'): https://getkirby.com/docs/reference/tools/data/encode

hi and thanks! can you give me a short example? i am a bit lost

Here is an example: Updating structure field inside a hook

Without any context, I don’t know what you are trying to do. There is no structure field in the example.

i would like to create strucrure fields from a database , but is not working.

like this with struture field too.

 <?php


    class CommentsPage extends Kirby\Cms\Page
    {
        public function children()
        {
            $comments = [];




            foreach (Db::select('AFILIADOS') as $comment) {

              $datos = [
                'nombre' => 'juan',
                'apellido' => 'perez',
              ];

                $comments[] = [
                    'slug'     => $comment->IDAFILIADO(),
                    'num'      => 0,
                    'template' => 'comment',
                    'model'    => 'comment',
                    'content'  => [
                        'nombre'  => $comment->NOMBRES(),
                        'apellido'  => $comment->APELLIDO(),
                        'datos' => Data::encode($datos, 'yaml'),

                      ]
                ];
            }

            return Pages::factory($comments, $this);
        }
    }

hi, this is the solution, thanks.

$datos = [
        [
        'nombre' => 'juan',
        'apellido' => 'perez',],
        [
        'nombre' => 'juan',
        'apellido' => 'perez',],
        [
        'nombre' => 'juan',
        'apellido' => 'perez',],
      ];
1 Like

Adding a structure field with Yaml::encode($array) worked for me only when I used a nested array like you did in your solution @diezmilseres.