【问题标题】:Laravel Eloquent ORM replicateLaravel Eloquent ORM 复制
【发布时间】:2016-07-01 11:32:07
【问题描述】:

我在使用所有关系复制我的一个模型时遇到问题。

数据库结构如下:

Table1: products
id
name

Table2: product_options
id
product_id
option

Table3: categories
id
name

Pivot table: product_categories
product_id
category_id

关系是:

  • product hasMany product_options
  • product belongsToMany 类别(通过 product_categories)

我想克隆一个包含所有关系的产品。目前这是我的代码:

$product = Product::with('options')->find($id);
$new_product = $product->replicate();
$new_product->push();
foreach($product->options as $option){
    $new_option = $option->replicate();
    $new_option->product_id = $new_product->id;
    $new_option->push();
}

但这不起作用(关系未克隆 - 目前我只是尝试克隆 product_options)。

【问题讨论】:

标签: php laravel eloquent clone relationship


【解决方案1】:

这在 5.5 上运行良好。 image , media 是一个关系名称。

$event = Events::with('image','media')->find($event_id);
            if($event){
                $newevent = $event->replicate();
                $newevent->push();
                 foreach ($newevent->getRelations() as $relation => $entries)
                 {
                    foreach($entries as $entry)
                    {
                        $e = $entry->replicate();
                        if ($e->push())
                        {
                            $newevent->{$relation}()->save($e);
                        }
                    }

                }                                

            }

【讨论】:

    【解决方案2】:
    $product = Product::with('options')->find($id);
    $new_product = $product->replicate();
    $new_product->{attribute} = {value};
    $new_product->push();
    
    $new_product->options()->saveMany($product->options);
    

    【讨论】:

    • 您应该解释您的答案,以便提问者(以及找到它的任何其他人)了解此代码的作用。
    • 以这种方式使用 saveMany 不会添加新记录,只会用新模型的值覆盖外键
    【解决方案3】:

    这段代码对我有用:

    $model = User::find($id);
    
    $model->load('invoices');
    
    $newModel = $model->replicate();
    $newModel->push();
    
    foreach($model->getRelations() as $relation => $items){
        foreach($items as $item){
            unset($item->id);
            $newModel->{$relation}()->create($item->toArray());
        }
    }
    

    从这里回答:Clone an Eloquent object including all relationships?

    这个答案(同样的问题)也很好用。

    //copy attributes from original model
    $newRecord = $original->replicate();
    // Reset any fields needed to connect to another parent, etc
    $newRecord->some_id = $otherParent->id;
    //save model before you recreate relations (so it has an id)
    $newRecord->push();
    //reset relations on EXISTING MODEL (this way you can control which ones will be loaded
    $original->relations = [];
    //load relations on EXISTING MODEL
    $original->load('somerelationship', 'anotherrelationship');
    //re-sync the child relationships
    $relations = $original->getRelations();
    foreach ($relations as $relation) {
        foreach ($relation as $relationRecord) {
            $newRelationship = $relationRecord->replicate();
            $newRelationship->some_parent_id = $newRecord->id;
            $newRelationship->push();
        }
    }
    

    从这里:Clone an Eloquent object including all relationships?

    根据我的经验,该代码适用于多对多关系。

    【讨论】:

      【解决方案4】:

      尝试使用attach 创建关系:

      foreach($product->options as $option){
          $new_option = $option->replicate();
          $new_option->save();
          $new_option_id = $new_option->id;
          $new_product->options()->attach($new_option_id);
      }
      

      【讨论】:

        猜你喜欢
        • 2015-12-06
        • 2013-02-12
        • 2016-09-24
        • 2013-12-28
        • 2019-08-01
        • 1970-01-01
        • 2015-05-16
        • 2016-08-21
        • 2018-01-09
        相关资源
        最近更新 更多