New Collection from array (worked on K2 not on K3)

Hi.

Trying to port all my code from a Kirby 2 project in to the new Kirby 3 platform.
This code worked on K2:

$selectedProductsCollection = new Collection($selectedProductsArray);
snippet('product-grid', array('products' => $selectedProductsCollection));

But in K3 I get this error: “Call to a member function id() on array” inside kirby/src/Cms/Collection.php
Tried to find a solution in the new doc but didn’t, so I ask here now.

it depends what is stored inside $selectedProductsArray and what happens in the snippet. please share the relevant parts as well.

Try:

$selectedProductsCollection = new Kirby\Toolkit\Collection($selectedProductsArray);

Thanks! That did the trick.

Ugh, that’s not good. We need to fix that. Could you create an issue for it?

Issue on GitHub created:

1 Like

This doesn’t appear to be working correctly for me unless I am misusing this class:

    $test_array = [
  'a',
  'b',
  'c',
  'd',
  'e',
  'f',
  'g'];

var_dump(new Kirby\Toolkit\Collection($test_array));

Results in:

object(Kirby\Toolkit\Collection)#887 (7) { [0]=> int(0) [1]=> int(1) [2]=> int(2) [3]=> int(3) [4]=> int(4) [5]=> int(5) [6]=> int(6) }

Creates a new Collection with the given objects not simple array.

new Collection(array $objects = [ ], object $parent = null)
1 Like
$coll = new Kirby\Toolkit\Collection($test_array);

foreach ($coll as $item) {
  echo $item;
}

//abcde

works. If you dump it, you only get the keys.

1 Like

Ah thank you both @texnixe and @ahmetbora , I was being dumb!