实体集合
Weaver\ORM\Collection\EntityCollection is a typed, immutable-by-default collection returned by repositories and query builders. Every method that would mutate the collection returns a new EntityCollection instance, so the original is never modified. This makes collections safe to pass around without defensive copying.
<?php
use Weaver\ORM\Collection\EntityCollection;
use App\Entity\Product;
// Collections are usually produced by repositories:
/** @var EntityCollection<Product> $products */
$products = $this->productRepository->findAll();
// You can also construct one manually:
$products = new EntityCollection([$product1, $product2, $product3]);
Iteration and access
count(): int
Returns the number of entities in the collection. EntityCollection also implements Countable, so count($collection) works too.
<?php
echo $products->count(); // 42
echo count($products); // 42
isEmpty(): bool / isNotEmpty(): bool
<?php
if ($products->isEmpty()) {
throw new \RuntimeException('No products found.');
}
if ($products->isNotEmpty()) {
$this->sendStockReport($products);
}