PHP code example of mcaskill / charcoal-model-collection
1. Go to this page and download the library: Download mcaskill/charcoal-model-collection library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mcaskill / charcoal-model-collection example snippets
$collection = new Collection([ $a, $b, $c, $d, $e ]);
// [
// 1 => Model (active: 1), 2 => Model (active: 0),
// 3 => Model (active: 1), 4 => Model (active: 1),
// 5 => Model (active: 0)
// ]
$filtered = $collection->filter(function ($obj, $id) {
return ($obj['active'] === true);
});
// [ 1 => Model, 3 => Model, 4 => Model ]
$repository = (new CollectionLoaderIterator)->setModel(Post::class);
$repository->addFilter('active', true)
->addFilter('published <= NOW()')
->setNumPerPage(10)
->setPage(3);
// Automatically find total count from query builder
$posts = $repository->load();
// query: SELECT SQL_CALC_FOUND_ROWS * FROM `charcoal_users` WHERE ((`active` = '1') AND (`published` <= NOW())) LIMIT 30, 10;
// query: SELECT FOUND_ROWS();
$total = $repository->foundObjs();
// int: 38
// Automatically find total count from query
$users = $repository->reset()->loadFromQuery('SELECT SQL_CALC_FOUND_ROWS * … LIMIT 0, 20');
// query: SELECT SQL_CALC_FOUND_ROWS * … LIMIT 0, 20;
// query: SELECT FOUND_ROWS();
$total = $repository->foundObjs();
// int: 38
// Automatically find total count from query
$users = $repository->reset()->loadFromQuery('SELECT * … LIMIT 0, 20');
// query: SELECT * … LIMIT 0, 20;
$total = $repository->foundObjs();
// LogicException: Can not count found objects for the last query
$repository = (new ModelCollectionLoader)->setModel(Post::class);
// …
$repository->setModel(Comment::class);
// RuntimeException: A model is already assigned to this collection loader: \App\Model\Post
$postsLoader = (new ModelCollectionLoader)->setModel(Post::class);
$commentsLoader = (clone $postsLoader)->setModel(Comment::class);
$posts = (new BaseCollectionLoader)->setModel(Post::class)->load();
// array: Post, Post, Post,…
($posts[0]->source() === $posts[2]->source())
// bool: false
$posts = (new ModelCollectionLoader)->setModel(Post::class)->load();
// array: Post, Post, Post,…
($posts[0]->source() === $posts[2]->source())
// bool: true
$repository = new ScopedCollectionLoader([
'logger' => $container['logger'],
'factory' => $container['model/factory'],
'model' => Post::class,
'default_filters' => [
[
'property' => 'active',
'value' => true,
],
[
'property' => 'publish_date',
'operator' => 'IS NOT NULL',
],
],
'default_orders' => [
[
'property' => 'publish_date',
'direction' => 'desc',
],
],
'default_pagination' => [
'num_per_page' => 20,
],
]);
$posts = $repository->addFilter('publish_date <= NOW()')->load();
// query: SELECT SQL_CALC_FOUND_ROWS * FROM `posts` WHERE ((`active` = '1') AND (`publish_date` IS NOT NULL) AND (`published` <= NOW())) ORDER BY `publish_date` DESC LIMIT 20;
$repository->reset()->load();
// query: SELECT SQL_CALC_FOUND_ROWS * FROM `posts` WHERE ((`active` = '1') AND (`publish_date` IS NOT NULL)) ORDER BY `publish_date` DESC LIMIT 20;
$repository = new ScopedCollectionLoader([…]);
$posts = $repository->withoutDefaults(function () {
$this->applyDefaultOrders();
$this->applyDefaultPagination();
})->load();
// query: SELECT SQL_CALC_FOUND_ROWS * FROM `posts` ORDER BY `publish_date` DESC LIMIT 20;