public function benchmark($mainCategoryNum, $subCategoryNum, $productsNum) { $totalIterations = $mainCategoryNum * $subCategoryNum * $productsNum; $this->setupDummyData($mainCategoryNum, $subCategoryNum, $productsNum); Product::enableLazyLoading(); Category::enableLazyLoading(); $time = microtime(true); $products = Product::find(); /** @var Product $product */ foreach ($products as $product) { $subCategory = $product->getCategory(); if ($subCategory) { $mainCategory = $subCategory->getCategory(); } $productName = $product->getName(); } echo $totalIterations . ' iterations with Lazy Loading enabled: ' . (microtime(true) - $time) . PHP_EOL; Product::disableLazyLoading(); Category::disableLazyLoading(); $time = microtime(true); $products = Product::find(); /** @var Product $product */ foreach ($products as $product) { $product->map(); $subCategory = $product->getCategory(); if ($subCategory) { $subCategory->map(); $mainCategory = $subCategory->getCategory(); } $productName = $product->getName(); } echo $totalIterations . ' iterations with Lazy Loading disabled: ' . (microtime(true) - $time) . PHP_EOL; }
public function testEagerLoadingOption() { $category = new Category(); $category->setName('Category 1'); $category->setDesc('Category 1 desc'); $category->save(); $product = new Product(); $product->setName('Product 1'); $product->setPrice(100); $product->setIsActive(true); $product->setCategory($category); $product->setCreatedAt(time()); $product->save(); Product::disableLazyLoading(); $this->assertFalse(Product::isLazyLoadingEnabled()); }