$this->find('foo')->find('awesome')->find('bar') • Each method call returns an instance of self/this allowing each to be called in sequence and modify each. 7 CC BY-NC 4.0 Justin Yost
$query, array $options = []) { if (!isset($options['tag'])) { throw new BadMethodCallException('Requires an option param of `tag` to be set'); } return $query ->matching('Tags', function ($q) use ($options) { return $q->where([ 'Tags.slug' => $options['tag'], ]); }); } 10 CC BY-NC 4.0 Justin Yost
Building useful reusable queries is easy • Building queries that are testable is easy • Building queries that follow best practices is easy 12 CC BY-NC 4.0 Justin Yost
from a Query. • Queries lazily executed, into an Iterator stored inside of the ResultSet • ResultSet stores the matching Entity instances for each result, as a Collection • Is Query truly a Collection - err no. 13 CC BY-NC 4.0 Justin Yost
or iterate over the results, Queries are not executed • Write a gazillion queries for a page, only talk to the database for the ones that actually are needed 14 CC BY-NC 4.0 Justin Yost
a pipeline of work on a Collection of Objects • Iterators stack, no matter if you call an iterator from multiple classes, iterators loop through the Collection of stuff, once • Lots of CakePHP 3 is Collections/Iterators • Perhaps even more importantly, lots of PHP stuff is using Iterators for lots of stuff 16 CC BY-NC 4.0 Justin Yost
Primary Registration for a Camp or a Coupon tied to the Order • ChildItems = Related Services for a Registration (TShirt, Lunches, etc) 17 CC BY-NC 4.0 Justin Yost
a single Item up to all the Orders in the database 2. My questions will change over time 3. My answers need to be live and relatively fast 4. I want this both in the user interface and make decisions using the answers 5. I don't want some MySQL dependent solution or writing custom queries (#2) 20 CC BY-NC 4.0 Justin Yost
Collection($this->items); // Create the anonymous function to sum up each Item, using the underlying // ItemEntity->{$itemSummerMethod}() $summer = function ($accumulated = 0.0, Item $item) use ($itemDetectorMethod) { return ($accumulated + $item->sum($itemDetectorMethod)); }; return $itemCollection->reduce($summer, 0.0); } 22 CC BY-NC 4.0 Justin Yost
than a findWhereBlah or get, uses a custom finder. • Collections are beyond useful, easily one of the most powerful pieces of CakePHP 3 in general • Iterators in general are the foundation of writing good pipelines of work needing to be done • https:/ /leanpub.com/iteratingphpiterators • http:/ /adamwathan.me/refactoring-to-collections/ 29 CC BY-NC 4.0 Justin Yost