Set an additional entry to a list

Hi folks, I just to set an additional entry on top of a list I get from the database and will use it to the view/template.

In View/Helper
public function getAccounts($year = null) {
$accountsTable = TableRegistry::get(‘Accounts’);
$query = $accountsTable->find(‘list’);
$query->where([‘year’ => $year])
->order([‘name’ => ‘ASC’])
->toArray();
return $query;
}

In Template (CTP-File)
$konten = $this->ViewTools->getAccounts($year);

$accountsArray = array_merge([‘00’ => ‘kein Eintrag’], $konten);

<?= $this->Form->input('searchname', ['options' => $accountsArray, 'default' => '00', 'label' => ' ', 'id' => 'account','class' => 'smallField', 'rel' => $costurl]); Error: Warning (2): array_merge(): Argument #2 is not an array [APP/Template\Element\searchAccount.ctp, line 6] The Selection-Field shows only the „00“. How do I manage to set an additional Entry on top oft the list?

Instead of

$query = $accountsTable->find(‘list’);
$query->where([‘year’ => $year])
    ->order([‘name’ => ‘ASC’])
    ->toArray();
return $query;

What you want is

return $accountsTable->find(‘list’)
    ->where([‘year’ => $year])
    ->order([‘name’ => ‘ASC’])
    ->toArray();

The former calls toArray, but does nothing with the results, so what you return from the function is still a query object, not an array. The latter returns the array, so that it’s what the array_merge call expects.

To debug things like this, when it tells you that argument 2 isn’t an array, you might use gettype right before the array_merge call, and then when that tells you that it’s an object call get_class. Then you’d know it’s a query object, and that helps point you in the right direction to solve it.

since you just want to add default item in select tag you can use empty option of

https://book.cakephp.org/3/en/views/helpers/form.html#options-for-select-checkbox-and-radio-controls

Hi folks, ad thanks for your reply. I have made it with the “return … ->toArray()” solution. I was not aware of the difference what is returned. Thanks again.