-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7619d21
commit 28e4d75
Showing
7 changed files
with
359 additions
and
307 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
} | ||
} |
Oops, something went wrong.