Retrieving Multiple Records AKA Collection

Retrieving Multiple Records By Primary Key List

Retrieving Multiple Records By Conditions


Almost all sql Select method is visible in Collection: select, distinct, calcFoundRows, where, orWhere, having, orHaving, orderBy, groupBy, limit, offset, join, leftJoin, rightJoin,
Sql querying in Collection is Lazy, it mean there are no query is executed until you interacting with the collection data. So you could adding more query filter to the collection later.
The Model Collection class is implemented of Countable, IteratorAggregate, JsonSerializable, ArrayAccess. So you could counting, iterate, encode as json or interacting it as array style.

Collection have 2 fallback method to model: save and delete and the model property setter.

Lazy Loading

Lazy Model support Lazy Loading. By default, the column types below considering as Lazy Loading:

  • Model::TEXT (text)
  • Model::MEDIUMTEXT (mediumtext)
  • Model::LONGTEXT (longtext)
  • Model::BLOB (blob)
  • Model::MEDIUMBLOB (mediumblob)
  • Model::LONGBLOB (longblob)
Feel free to custom you own lazy loading column by defining property $defaultLazyLoadColumnTypes or $defaultSelectColumns in your model definition.

As you see. The column content of posts table not selected yet. But when the first time you get the content property, it will select the content column related to the collection.

Eager Loading

The Eager Loading existing to resolve the N + 1 query problem. For example, in the Defining Models, the Post model related to User model specified by Creator. See an example above, we retrieving the creator name of post collection:

.

Assume the posts table have 100 records.
Normally, you are guessing there are total 101 queries executed. 1 is for the posts list, and 100 queries for each post to retrieving the creator name.
Actually, there are just 2 queries executed:


Unlike some ORM libraries, they implemented Eager Loading by style look like this: Lazy ORM do this automatically. No need to call something like with('Creator')


comments powered by Disqus