簡體   English   中英

Laravel Eloquent ORM復制

[英]Laravel Eloquent ORM replicate

我在使用所有關系復制我的一個模型時遇到問題。

數據庫結構如下:

Table1: products
id
name

Table2: product_options
id
product_id
option

Table3: categories
id
name

Pivot table: product_categories
product_id
category_id

關系是:

  • 產品有很多product_options
  • 產品屬於多種類別(槽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)。

這段代碼對我有用:

$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());
    }
}

從這里回答: 克隆一個包含所有關系的雄辯對象?

這個答案(同樣的問題),也可以正常工作。

//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();
    }
}

從這里: 克隆一個包含所有關系的雄辯對象?

根據我的經驗,代碼適用於多對多的關系。

嘗試使用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);
}
$product = Product::with('options')->find($id);
$new_product = $product->replicate();
$new_product->{attribute} = {value};
$new_product->push();

$new_product->options()->saveMany($product->options);

這在5.5上工作正常。 圖像,媒體是一個關系名稱。

$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);
                        }
                    }

                }                                

            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM