1

I'm having a small issue creating a script that take in an entry_id, and based on that creates a new carbon copy entry with a new entry_id.

I've got this working, however as it stands now i'm forced to updated the $entry_data array everytime a new ee_channel_data field_id_# column is created.

ee()->load->library('api');
ee()->api->instantiate('channel_entries');
ee()->api->instantiate('channel_fields');

$entry_data = array(
    'title' =>  $get_channel_title_res['title'] . ' COPY',
    'url_title' =>  $get_channel_title_res['url_title'] . '_copy',
    'entry_date' => $get_channel_title_res['entry_date'],
    'edit_date' =>  $get_channel_title_res['edit_date'],
    'field_id_1' => $get_channel_data_res['field_id_1'],
    'field_id_2' => $get_channel_data_res['field_id_2'],
    'field_id_3' => $get_channel_data_res['field_id_3']
);

ee()->api_channel_fields->setup_entry_settings($channel_id, $entry_data);               

$success = ee()->api_channel_entries->save_entry($entry_data, $channel_id);

I would prefer not to have to specify the fields, but simply tell EE's API Create a new entry, based on this old entry, and change it's title/url_title to appent _COPY.

I realize there's an addon for this, however that's not an option for me

Thanks!

0

Have you considered an answer like this?

$entry_data = array();

// select all column names from your exp_channel_data table
$col_names = ee()->db->query('SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='yourdatabasename' AND `TABLE_NAME`='exp_channel_data')->result_array();

foreach ($col_names as $column_name) {
    // conditionals
    switch ($column_name) {

        // don't add an entry_id, 'cause auto insertion (giggity)
        case "entry_id":
            break;

        // title string append
        case "title":
            $entry_data[$column_name] = $get_channel_title_res[$column_name] . ' COPY';
            break;

        // url_title string append
        case "url_title";
            $entry_data[$column_name] = $get_channel_title_res[$column_name] . '_copy';

        // add everything else
        default:
            if (isset($get_channel_title_res[$column_name])) {
                $entry_data[$column_name] = $get_channel_title_res[$column_name];
            }
    }
}

// continue along your merry way with your api wackery

Edit

There isn't a built in function to do what you're asking.

OK, what about something even simpler? Assuming you have the entry_id you want to clone...

// get old data
$entry_data = ee()->db->select()
                      ->from('exp_channel_data')
                      ->where('entry_id', $entry_id)
                      ->get()->result_array();

// fix our copy
$entry_data["entry_id"]   = "";
$entry_data["title"]     .= " COPY";
$entry_data["url_title"] .= "_copy";

// move along, nothing to see here

Adds a database call but looks nicer!

| improve this answer | |
  • I have, this was plan-B. I was just hoping that there's a cleaner way to do this via API call like ee()->api_channel_entries->copy_from($thisentry); – ipixel Oct 27 '15 at 19:37
  • There isn't that functionality built in to the EE channel entries api. Sorry. – jrothafer Oct 27 '15 at 19:56
  • and there is this devot-ee.com/add-ons/mx-cloner – Jim Wyse Oct 27 '15 at 22:01
  • Yea, we try to stay away from addons, modules and extensions. I saw mx-cloner when researching how to duplicate entries. But that was not an option. Thanks though! – ipixel Oct 28 '15 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.