We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

before_channel_entry_save hook - Forcing entry to save as new

Developer Preview

TJ Draper's avatar
TJ Draper
222 posts
8 years ago
TJ Draper's avatar TJ Draper

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);
}
       
Kevin Cupp's avatar
Kevin Cupp
791 posts
8 years ago
Kevin Cupp's avatar Kevin Cupp

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);
       
TJ Draper's avatar
TJ Draper
222 posts
8 years ago
TJ Draper's avatar TJ Draper

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.

       
Kevin Cupp's avatar
Kevin Cupp
791 posts
8 years ago
Kevin Cupp's avatar Kevin Cupp

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).

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.