stibium/phalcon.eager-loading 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

stibium/phalcon.eager-loading

Composer 安装命令:

composer require stibium/phalcon.eager-loading

包简介

Solves N+1 query problem in Phalcon Model

关键字:

README 文档

README

This package provides eager-loading support for Phalcon 1.3.* - 2.0.*. Requires PHP 5.6, for PHP 5.3 support use php-5.3 branch

Usage

The usage is similar to Laravel, I've implemented in a trait with and load methods, so within a model that uses that trait (Sb\Framework\Mvc\Model\EagerLoadingTrait) you can do:

<?php
use Sb\Framework\Mvc\Model\EagerLoading\Loader,
	Sb\Framework\Mvc\Model\EagerLoading\QueryBuilder;

$robotsAndParts = Robot::with('Parts');

// Equivalent to:

$robots = Robot::find();
foreach ($robots as $robot) {
	$robot->parts; // $robot->__get('parts')
}

// Or

$robot = Robot::findFirst()->load('Parts');

// Equivalent to:

$robot = Robot::findFirst();
$robots->parts; // $robot->__get('parts')

// Because Robot::find() returns a resultset, so in that case this is solved with:
$robots = Loader::fromResultset(Robot::find(), 'Parts'); # Equivalent to the second example

// Multiple and nested relations can be used too
$robots = Robot::with('Parts', 'Foo.Bar');

// And arguments can be passed to the find method
$robots = Robot::with('Parts', 'Foo.Bar', ['limit' => 5]);

// And constraints
$robots = Robot::with(
	[
		'Parts',
		'Foo.Bar' => function (QueryBuilder $builder) {
			// Limit Bar
			$builder->limit(5);
		}
	],
	[
		'limit' => 5
	]
);

// constraints with the Loader too
$robots = Loader::fromResultset(Robot::find(), [
        'Foo.Bar' => function (QueryBuilder $builder) {
			$builder->where('Bar.id > 10'); 
	             }
]); 

For more examples, return types etc visit the tests folder or take a look at the code, it's quite small.

License

The Unlicense

统计信息

  • 总下载量: 9.17k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 37
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 36
  • Watchers: 3
  • Forks: 24
  • 开发语言: PHP

其他信息

  • 授权协议: Unlicense
  • 更新时间: 2015-05-28