Initialize database error

I am using a route to initialize a database:

 [
      'pattern' => 'initdb',
      'action' => function() {
        $db = Db::connect();

        $votes = 'CREATE TABLE votes(id INT, video VARCHAR(10), rate VARCHAR(10), PRIMARY KEY (id))';

        if ($db->validateTable('votes')) { // if table exists
          $db->dropTable('votes'); // drop it
          $db->query($votes); // create a blank one
        } else {
          $db->query($votes); // if it doens't exist, create it
        }
                
        return 'database initialized';
      }
    ]

Upon visiting this route, the first time I get the error “array_search() expects parameter 2 to be array, string given”, and if I visit it again, it works as epxected. What am I doing wrong?

There is no error in your code, This is a Kirby known issue:

As workaround, you can change/replace following line:

With:

$key = array_search($table, $this->tableWhitelist ?? []);
1 Like

Coverage PRs can take a long time to merge, so I opened a separate PR:

https://github.com/getkirby/kirby/pull/2788

Thanks, that worked!