How would one go about making a channel entry save as new? I have set the entry_id to null on the entry and the field facades. I feel like that ought to get me dang close but I get cryptic errors and nothing is saved.
// Set content ID null
$entry->setId(null);
// Iterate through custom fields and set content ID to null
foreach ($entry->getCustomFields() as $customField) {
/** @var FieldFacade $customField */
$customField->setContentId(null);
}
Primary keys are currently considered immutable. If you’re wanting to make a copy of a model, try filling a new one with the values of the one you want to copy, but unset the primary key:
$values = $old_model->getValues();
unset($values['id']);
$clone = ee('Model')->make('SomeModel', $values);
Sweet. Getting closer. I have the new entry saving, but I need to figure out how to populate the custom fields from the old entry. I can see the getCustomFields()
method which returns an array of EllisLab\ExpressionEngine\Model\Content\FieldFacade
classes. But I can’t figure out how to set those from the old entry.
I would think you wouldn’t need to, just need to set the data. For instance, we just do $entry->set($_POST);
when we save a new entry from the publish form and the models take care of the rest. Maybe try explicitly calling set()
like that, loading in the data via make()
sometimes doesn’t do all that’s needed. I would think it should work for any fieldtype storing data in the channel_data table (so not Relationships or Grid).
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.