wojtek77/phalcon.eager-loading

解决 Phalcon 模型(针对 Phalcon 4)中的 N+1 查询问题

dev-master 2022-04-21 09:11 UTC

This package is auto-updated.

Last update: 2024-09-29 05:55:33 UTC


README

此包为 Phalcon 4.* 提供预加载支持,需要 PHP 7.2。

对于 Phalcon 1.3.* - 2.0.* 使用 phalcon_1-2 分支。

安装

composer require wojtek77/phalcon.eager-loading:dev-master

使用

使用方式与 Laravel 类似,我在 traits 中实现了 withload 方法,因此在一个使用该 traits 的模型(Sb\Framework\Mvc\Model\EagerLoadingTrait)中,你可以这样做

<?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'); 
	             }
]); 

与软删除一起使用

默认情况下,不包含软删除条目的预加载操作在 'delete_date' 字段上,其中 NULL 值表示未删除的记录。

可以通过将以下内容添加到配置中全局更改此行为

'eagerLoadingSoftDelete' => [
    'name' => 'delete_date', //name of flag column for "soft delete"
    'value' => null, //value of a flag column for not deleted record
]

此外,还有可能只为特定模型更改软删除字段值

<?php
use Phalcon\Mvc\Model;

class Part extends Model
{
    private $__eagerLoadingSoftDeleteName = 'delete_date'; //name of flag column for "soft delete"
    private $__eagerLoadingSoftDeleteValue = null; //value of a flag column for not deleted record
}

软删除的使用

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

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


// Because Robot::find() returns a resultset, so in that case this is solved with:
$robots = Loader::fromResultsetWithoutSoftDelete(Robot::find(), 'Parts');

有关更多示例和返回类型等,请访问测试文件夹或查看代码,它相当小。

许可证

Unlicense