Skip to content

Commit

Permalink
优化代码,返回值调整为collection对象
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Sep 19, 2019
1 parent 7619d21 commit 28e4d75
Show file tree
Hide file tree
Showing 7 changed files with 359 additions and 307 deletions.
100 changes: 99 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,99 @@
# phalcon-eagerload


### phalcon-eagerload

> 允许phalcon进行数据预加载的扩展


## 安装

```bash
composer require sowork/phalcon-eagerload
```



## 使用

* 在你的Model中引入 `Sowork\EagerLoad\Traits\EagerLoadingTrait` 文件,并定义相对的关联关系

```php
<?php

namespace Phalcon\Mvc\Model;
use Sowork\EagerLoad\Traits\EagerLoadingTrait;

/**
* Class User
* @package App\Models
*/
class Book extends Model
{
use EagerLoadingTrait;
public function getSource()
{
return 'users';
}

public function initialize()
{
parent::initialize();
$this->belongsTo( 'id', App\Author::class,'bookId', [
'alias' => 'blogs'
]);
}
}
```



* 使用with()方法对数据进行预加载

```php
// 加载单个关联关系
$books = App\Book::with('author')->findFirst([
'conditions' => 'id = 10',
]);

// 加载多个关联关系
$books = App\Book::with('author', 'publisher')->find();

// 加载嵌套关联关系
$books = App\Book::with('author.contacts')->find();

// 加载带条件约束的关联关系
$users = App\User::with(['posts' => function ($query) {
$query->where('title', 'like', '%first%');
}])->find();
```



* 使用load()方法对数据进行懒惰渴求式加载
```php
// 这在你需要动态决定是否加载关联模型时可能很有用
$books = App\Book::find(); // or App\Book::findFirst()

if ($someCondition) {
$books->load('author', 'publisher');
}

// 也可以通过条件限制

$books->load(['author' => function ($query) {
$query->orderBy('published_date', 'asc');
}]);

```


### 返回结果

* 根据列表或者对象,分别会返回 `Phalcon\Mvc\ModelInterface``Tightenco\Collect\Support\Collection` 对象



### 集合操作

> 当返回结果是一个集合时,更方便我们对数据结果进行处理,具体集合的操作方法请看[相关文档](#https://xueyuanjun.com/post/19507.html)
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
"email": "[email protected]"
}
],
"require": {},
"require": {
"tightenco/collect": "^6.0"
},
"autoload": {
"psr-4": {
"Sowork\\EagerLoad\\": "src/"
}
},
"require-dev": {
"phalcon/dd": "^1.1"
}
}
38 changes: 38 additions & 0 deletions src/EagerLoadServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Sowork\EagerLoad;

use Phalcon\Di\ServiceProviderInterface;
use Phalcon\DiInterface;
use Sowork\EagerLoad\Model\EagerLoading\QueryBuilder;
use Tightenco\Collect\Support\Collection;

class EagerLoadServiceProvider implements ServiceProviderInterface
{
public function register(DiInterface $di)
{
$this->addCollectionMethods();
}

public function addCollectionMethods()
{
Collection::macro('load', function($relations){
/**
* Load a set of relationships onto the collection.
*
* @param mixed $relations
* @return $this
*/
if ($this->isNotEmpty()) {
if (is_string($relations)) {
$relations = func_get_args();
}

return (new QueryBuilder())->with(
array_merge([$this, get_class($this->first())], is_string($relations) ? func_get_args() : $relations)
);
}
return $this;
});
}
}
Loading

0 comments on commit 28e4d75

Please sign in to comment.