为什么laravel模型会复制数据集以及如何(如果可能)只有一组数据?

cyt*_*nny 15 php model object query-builder laravel

laravel模型提供了一种方法,它可以返回来自另一个关联表的结果.

例如,我有一个名为item的表和另一个名为feedback的表,其中反馈表存储项表中项的反馈.因此,为了得到id为1的项目的所有反馈,我将做:

Item::find(1)->feedback;
Run Code Online (Sandbox Code Playgroud)

以下是返回对象的打印输出.

Illuminate\Database\Eloquent\Collection Object
(    [items:protected] => Array
       (
           [0] => Feedback Object
               (
                   [table:protected] => feedback
                   [connection:protected] => 
                   [primaryKey:protected] => id
                   [perPage:protected] => 15
                   [incrementing] => 1
                   [timestamps] => 1
                   [attributes:protected] => Array
                       (
                           [id] => 1
                           [rma_id] => 3
                           [item_id] => 8
                           [quo_id] => 0
                           [case_id] => i2eM20160120
                           [line_no] => 000001
                           [content] => test
                           [status] => sent
                           [read] => 0
                           [sender] => Tester
                           [created_at] => 2016-01-20 18:03:44
                           [updated_at] => 2016-01-20 18:03:44
                       )

                   [original:protected] => Array
                       (
                           [id] => 1
                           [rma_id] => 3
                           [item_id] => 8
                           [quo_id] => 0
                           [case_id] => i2eM20160120
                           [line_no] => 000001
                           [content] => test
                           [status] => sent
                           [read] => 0
                           [sender] => Tester
                           [created_at] => 2016-01-20 18:03:44
                           [updated_at] => 2016-01-20 18:03:44
                       )

                   [relations:protected] => Array
                       (
                       )

                   [hidden:protected] => Array
                       (
                       )

                   [visible:protected] => Array
                       (
                       )

                   [appends:protected] => Array
                       (
                       )

                   [fillable:protected] => Array
                       (
                       )

                   [guarded:protected] => Array
                       (
                           [0] => *
                       )

                   [dates:protected] => Array
                       (
                       )

                   [touches:protected] => Array
                       (
                       )

                   [observables:protected] => Array
                       (
                       )

                   [with:protected] => Array
                       (
                       )

                   [morphClass:protected] => 
                   [exists] => 1
               )

       )

)
Run Code Online (Sandbox Code Playgroud)

它工作正常,它表明对id为1的项目只有一个反馈.

我担心的是,该数据集的复制[attributes:protected][original:protected].这只是一个测试案例,真实案例将包含数千个反馈,并且重复数据集会浪费大量内存.如果我使用这种DB::table('table_name')方法,数据集不会重复,但这样不太方便.

为什么laravel需要复制模型中的数据?

有没有办法让它只返回一组数据?

目前我正在使用->toArray()查询后立即减少不必要的数据,但内存使用仍然存在,因为laravel仍在创建该组数据.

Th3*_*ist 5

虽然很难得到一个好例子,但它允许您在明确保存之前设置属性.如果你经历了很多功能并最终检查是否所有内容都已正确设置以进行最终保存而不需要将所有内容存储在单独的变量中,这可能会很好.

非常小的例子:

$user = User::find(1);
print_r($user);
$user->name = 'John Doe';
print_r($user);
$user->save();
print_r($user());
Run Code Online (Sandbox Code Playgroud)

返回类似于:

第一次印刷:

[attributes:protected] => Array
(
   [id] => 1
   [name] => 'Jimmy Doe'
   ...
)
[original:protected] => Array
(
   [id] => 1
   [name] => 'Jimmy Doe'
   ...
)
Run Code Online (Sandbox Code Playgroud)

第二次印刷:

[attributes:protected] => Array
(
   [id] => 1
   [name] => 'John Doe'
   ...
)
[original:protected] => Array
(
   [id] => 1
   [name] => 'Jimmy Doe'
   ...
)
Run Code Online (Sandbox Code Playgroud)

三维印刷:

[attributes:protected] => Array
(
   [id] => 1
   [name] => 'John Doe'
   ...
)
[original:protected] => Array
(
   [id] => 1
   [name] => 'John Doe'
   ...
)
Run Code Online (Sandbox Code Playgroud)

只有在save()之后,数据才会真正保存到DB中.

当模型是save()时,Eloquent的syncOriginal()会被激活:

/**
 * Sync the original attributes with the current.
 *
 * @return $this
 */
public function syncOriginal()
{
    $this->original = $this->attributes;

    return $this;
}
Run Code Online (Sandbox Code Playgroud)