In my CakePHP 3 app, I have a somewhat elaborate tree of entities that I need to clone and save.
The root of the structure is a Questionnaire, a Questionnaire hasMany Questions, each Question hasMany Fields, etc. (it goes deeper). Now I want the user to be able to define a new questionnaire by copying an old one. Then they can change whatever they need.
I can get a dump of what I need to copy by using $questionnaire->$this->Questionnaires->get($id)
with the appropriate contain fields. Is there a clever way to save this as a bunch of new entities while preserving the data and the structure between them?
I think the best possible way would be following work flow:
$this->Questionnaires->newEntity($arrayData, ['associated' => ['Questions', '...']]);
AFAIK there's no "smarter" way of cloning an entity with associations in Cake 3 :-)
You could also use this plugin. You only have to configure the behavior and it gives you other functionalities, like setting default values or appending text to some field.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With