XF 2.0 Using array_shift on an ArrayCollection?

Jaxel

Well-known member
Fetching a thread's posts returns an ArrayCollection. I am trying to pop off the first element in the ArrayCollection using array_shift. Unfortunately this doesn't work because the entity finder returns an ArrayCollection rather than an array.

What would be the best way to accomplish this instead?
 
Have you considered looking in the code? In your IDE, you should be able to see autocomplete for the methods on an ArrayCollection, but failing that, you can search for the FinderCollection or ArrayCollection classes, which extends the AbstractCollection class, which will lead you to XF\Mvc\Entity\AbstractCollection in the files.

Once you have found that class, you will find public function shift($returnType = 'entity'). If you look in the source code for that function, you can determine what $returnType parameter fits your needs.


Fillip
 
Have you considered looking in the code? In your IDE, you should be able to see autocomplete for the methods on an ArrayCollection, but failing that, you can search for the FinderCollection or ArrayCollection classes, which extends the AbstractCollection class, which will lead you to XF\Mvc\Entity\AbstractCollection in the files.

Once you have found that class, you will find public function shift($returnType = 'entity'). If you look in the source code for that function, you can determine what $returnType parameter fits your needs.


Fillip
AbstractCollection->shift() doesn't function the same way array_shift() works.

array_shift() returns the first value in an array, AND removes the first value from the array.
AbstractCollection->shift() returns the first value in the collection, but does NOT remove the first value from the array.

shift() seems to pretty much be the same as first()
 
AbstractCollection->shift() doesn't function the same way array_shift() works.

array_shift() returns the first value in an array, AND removes the first value from the array.
AbstractCollection->shift() returns the first value in the collection, but does NOT remove the first value from the array.

shift() seems to pretty much be the same as first()
While it may be sub-optimal to do it over two lines instead of one, nothing stops you from running $collection->first() then $collection->shift(false); :)


Fillip
 
While it may be sub-optimal to do it over two lines instead of one, nothing stops you from running $collection->first() then $collection->shift(false); :)


Fillip
That worked... but I noticed $collection->shift(false) actually changes all the entity keys...

I figured it out using $collection->slice(1) instead.
 
Back
Top Bottom