jamesaspence/grandiloquent

此包已被废弃,不再维护。未建议替代包。

Grandiloquent 是 Laravel 精美的 Eloquent ORM 的扩展,旨在通过提高效率来改变一些事物。主要,这些更改包括分组查询和其他更有效的 SQL 使用。

1.1.4 2015-10-02 21:47 UTC

This package is not auto-updated.

Last update: 2020-01-24 15:51:50 UTC


README

Grandiloquent 是 Eloquent 的简单优雅扩展,使得数据库的大量写入更高效。我的整个理念是确保 Eloquent 方法,如 saveMany 和 push,在数据库上更高效。

仅限 MySQL

此包目前仅适用于 MySQL。它使用原始查询来实现这里的功能。最终我希望支持 Laravel 所支持的所有数据库类型,但目前还不是这样。

安装

可以通过在 composer.json 的 require 中添加以下行来完成安装

"jamesaspence/grandiloquent": "1.*"

##配置

您需要通过以下方式之一:A) 扩展 GrandModel 类,或将 GrandModelTrait 添加到您的 Eloquent 类中。想法是使此配置尽可能简单。

基本用法

Grandiloquent 将直接集成到当前的工作流程中。例如,假设您已定义了如下模型系统

<?php

use Grandiloquent/GrandModel;

class Book extends GrandModel
{
    public function chapters()
    {
        return $this->hasMany(Chapter::class);
    }
     
}

然后您的 Chapter 模型

<?php

use Grandiloquent/GrandModel;

class Chapter extends GrandModel
{
    public function book()
    {
        return $this->belongsTo(Book::class);
    }
}

使用 Grandiloquent,为了将模型数组直接保存到它们的父对象(在本例中为 Book),您可以调用与 Eloquent 相同的函数

$book = Book::find(1);
$chapters = [];
for($i = 0; $i < 5; ++$i)
{
    $chapter = new Chapter();
    $chapter->name = "Chapter $i";
    $chapters[] = $chapter;
}
$book->chapters()->saveMany($chapters);

您还可以直接在集合上调用保存,如下所示。

/** @var GrandCollection $books */
$books = Book::take(10)->get();
$books->saveMany();

这些方法将作为单个查询执行所有更新。然而,插入仍然是单独进行的。