Duplicate or export data when used in custom plugin #68
Replies: 7 comments 10 replies
-
I see a similar request here #57 |
Beta Was this translation helpful? Give feedback.
-
If you use Boxes in your own code, it should be as easy as: $copy = YourModel::find(1)->replicateWithRelations();
$copy->save(); October does all the heavy lifting in this case: It will copy all the Content models, all the Boxes attached to that as well as any file attachements to any of the Boxes. |
Beta Was this translation helpful? Give feedback.
-
Thank you for replying so quickly, appreciate the feedback! Unfortunately I can't get that to work and I wish it would be so simple. Removing all other relations on the model and leaving just the morphOne relation to the Content-model class gives the error
So something is acting up, and I suspect it's looking for boxes-relations that isn't there. Is there an issue with this being added in a custom plugin and the Content-model is actually referencing itself as a relation? I see there is a comment in the model regarding $this->content? I also tried something along the lines of replicating the original model with just Any help would be greatly appreciated :) |
Beta Was this translation helpful? Give feedback.
-
Here's my model definition. When testing I've removed any other relations on the model. I'll post the stracktrace shortly :) <?php
namespace Lamme\Travel\Models;
use Model;
use OFFLINE\Boxes\Models\Page;
/**
* Trip Model
*
* @link https://docs.octobercms.com/3.x/extend/system/models.html
*/
class Trip extends Model
{
use \October\Rain\Database\Traits\Validation;
use \October\Rain\Database\Traits\Sluggable;
/**
* @var string table name
*/
public $table = 'lamme_travel_trips';
public $implement = ['Rainlab.Location.Behaviors.LocationModel'];
/**
* @var array rules for validation
*/
public $rules = [];
/**
* @var array Generate slugs for these attributes.
*/
protected $slugs = ['slug' => ['title']];
/**
* @var array Repeater fields.
*/
protected $jsonable = ['schedule', 'content', 'intro', 'meta'];
/**
* Relations
*/
public $morphOne = [
'trip_boxes' => [ // Any name you want
\OFFLINE\Boxes\Models\Content::class,
'name' => 'content' // Has to be called 'content'
]
];
public $hasMany = [
'trip_instances' => \Lamme\Travel\Models\TripInstance::class
];
public $belongsToMany = [
'destinations' => [
\Lamme\Travel\Models\Destination::class,
'table' => 'lamme_travel_destination_trip',
'key' => 'trip_id',
'otherKey' => 'destination_id'
],
'trip_categories' => [
\Lamme\Travel\Models\TripCategory::class,
'table' => 'lamme_travel_trip_trip_category',
'key' => 'trip_id',
'otherKey' => 'trip_category_id'
],
'trip_types' => [
\Lamme\Travel\Models\TripType::class,
'table' => 'lamme_travel_trip_trip_type',
'key' => 'trip_id',
'otherKey' => 'trip_type_id'
]
];
public $hasManyThrough = [
'countries' => [
\RainLab\Location\Models\Country::class,
'through' => \Lamme\Travel\Models\Destination::class
],
];
public function scopePublished($query)
{
return $query->where('is_published', 1);
}
} |
Beta Was this translation helpful? Give feedback.
-
Stacktrace
|
Beta Was this translation helpful? Give feedback.
-
I tried reproducing the issue with the following controller code: public function onDuplicate()
{
$ids = input('checked', []);
$models = MyModel::find($ids);
$models->each(function($model) {
$copy = $model->replicateWithRelations();
$copy->save();
});
} This works without any issue. All Content and Box models are duplicated. Can you check if this works for you? If not, could you upgrade to Boxes 3.0 and see if this solves the issue? If not, would it be OK for you to send me a copy of your plugin code to tobias at offline dot ch? Maybe I can spot the issue if I am able to reproduce the issue. |
Beta Was this translation helpful? Give feedback.
-
Sorry for my late reply, and thank you for following up. I've updated now to the 3.0 version and things are indeed working :) Happy days! ;) |
Beta Was this translation helpful? Give feedback.
-
First, thank you for an amazing plugin!
I've implemented the editor in my own plugin, and it works great. But I have one limitation that has me stumped. I would really like a simple way of duplicating content to a new model.
My use case is as follows. I have a model set up with a $morphOne relation to the content model as per the documentation. Is there any way to do a simple "replicate" on the content models boxes relation? Maybe it's late and my brain is fried, but I would really appreciate input.
If there is no easy way of duplicating the boxes on the related model (I was getting confused with the nesting) it would be great to have a way of exporting the boxes content to json or in a simple matter be able to mass-create boxes that takes care of nesting and relations. I'm by far no expert, so maybe this is a basic thing I've overlooked, and if so I apologise.
Beta Was this translation helpful? Give feedback.
All reactions