hroma
October 30, 2022, 4:11pm
1
Hallo,
I’m doing something wrong but can’t figure it out and I need your help.
I’m trying to generate a select field (on panel side) from database entries but I’m getting
Call to a member function values() on array
my class User/function getUser generate the following output:
Array
(
[0] => Kirby\Toolkit\Obj Object
(
[id] => SBBkpjmHKS4wqFl6
[email] => email@email.com
[firstname] => firstname
[lastname] => lastname
[password] => password
[language] => en
)
)
then I try to pass it to my field like this:
'type' => 'select',
'options' => User::getUser()->values(fn($user) => [
'value' => $user->id(),
'text' => $user->email()
])
Thank you in advance for your help.
Could you provide reproducible code please?
hroma
October 30, 2022, 6:06pm
3
The request is quite the same as I made in a previous topic but instead of getting the languages options I need to retrieve user information from a database (sqlite) with a “value” =>" userInDatabase.id()" and “text” => userInDatabase.email to generate the dropdown.
So… following the Extended products area demo code , I would add inside classes/Product.php the follwing:
public static function getUser(): array
{
Db::connect([
'database' => dirname(__DIR__, 1).'/db/user.sqlite',
'type' => 'sqlite'
]);
$customers = Db::select('users', '*', null, 'email DESC')->toArray();
return array_map(function($customer) {
return (object) $customer;
}, $customers);
}
and adding a field inside dialogs/fields.php :
'users' => [
'label' => 'Users',
'type' => 'select',
'options' => Products::getUser()->values(fn($user) => [
'value' => $user->id(),
'text' => $user->email()
])
],
hroma
October 30, 2022, 6:16pm
4
or even an easier way… in Extended products area demo code… in dialogs/fields.php instead of passing a simple array from classes/Products.php with public static function types(): array how to pass $key and $value instead of simply $value?
Something like:
Dialogs/fields.php:
'options' => A::map(Product::types(), function ($type) {
return ['value' => $key, 'text' => $value];
})
Array returned by classes/Products.php public static function types() :
return [
'0' => 'bakery'
'1' => 'dairy',
'2' => 'fruit',
'3' => 'meat',
'4' => 'vegan',
'5' => 'vegetable',
];
What I don’t understand is the code in your getUser
method.
Why don’t you simply return
public static function getUser(): array
{
Db::connect([
'database' => dirname(__DIR__, 1).'/db/user.sqlite',
'type' => 'sqlite'
]);
$return = Db::select('users', '*', null, 'email DESC');
}
Then:
'options' => User::getUser()->values(fn($user) => [
'value' => $user->id(),
'text' => $user->email()
])
This will return a collection on which you can then call the values()
method.
Instead, your method returns an array on which you cannot call values()
hroma
October 30, 2022, 6:54pm
6
I think, I’ve tried so many variants to get the expected result that I’ve over complicated the code (and it’s Sunday and I’m tired)…
I’ve implemented your solution and I got:
Return value must be of type array, Kirby\Toolkit\Collection returned
Got to change your return value from array to Kirby\Toolkit\Collection
hroma
October 30, 2022, 7:01pm
8
I’ve removed :array and it’s working… Shouldn’t I return an array in the class?
Depends on your use case.
If you return an array, you cannot use values()
further down the line but would have to use A::map
like in the recipe.
hroma
October 30, 2022, 7:06pm
10
@texnixe Thank you sooooo much! I’m twice grateful… for the quick responses and for responding a Sunday!
1 Like