3

We are using expressionengine as a cms. I'm trying to split a big channel into separate channels. At the moment we have several events with an agenda (which is a matrix field). Now I'm trying to "migrate" or "move" the complete matrix field into an own channel called "agenda" and replace it with an playa field in the original "events" channel.

Does anybody has a suggestion to do this?

5
  • how many entries you have?
    – Max Lazar
    Feb 3, 2014 at 14:23
  • There are a plenty. I don't want to do it manually. Each of the 200 entries have 20 entries. But I think it's a general question how to move fields with the content to another channel.
    – Pibble
    Feb 4, 2014 at 7:03
  • How your progress?
    – Max Lazar
    Feb 5, 2014 at 16:13
  • Didn't try it out, yet. I optimized the Playa fields and this worked for me (limiting the initial results for each field to 25 entries). Nevertheless I think it is a great idea for a add-on which has the ability to move channel fields with their content to other channels and optionally insert a relationship field in the original channel entry.
    – Pibble
    Feb 6, 2014 at 15:40
  • too many options needs to foresee and it's not mass-product. So it will be too expensive add-on :) . Cheeper to hire person who will write migration script for specific needs.
    – Max Lazar
    Feb 6, 2014 at 15:51

2 Answers 2

1

The way I do this is via phpmyadmin. In exp_channel_fields change the group id to the group you would like it to be in. Hope this helps

2
  • Pierre want not just reassign field, but also move exist data to new entrees and link old one to new entry with playa. So change field link to field group is most simple operation from this.
    – Max Lazar
    Feb 3, 2014 at 14:22
  • Changing the group_id would be helpful for the channel field itself. But the data (especially for matrix) would not be affected.
    – Pibble
    Feb 4, 2014 at 7:04
1

I can't give you a fish, but can show direction. Not sure regarding Playa part (don't have it) but something like this (make sure that you have DB backup before run this code), run from EE template:

<?php 

$new_channel_id = 2;
$new_field_group = 2;
$field_id = 10;
$playa_field_id = 11;

// array where we keep rel between old entry and new one (for playa)
$entry_rel = array();

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

#get all matrix records
$query =$this->EE->db->query( "SELECT DISTINCT(entry_id)
                                           FROM exp_matrix_data
                                           WHERE field_id = $field_id
                                           " );

if ($query->num_rows()) {
    foreach ($query->result()  as $field)
    {
        //create new entry
        $data = array(
            'title'         => 'my new  entry with matrix',
            'entry_date'    => time(),
            'edit_date'     => time()
        );

        if (ee()->api_channel_entries->submit_new_entry($new_channel_id, $data) === FALSE)
        {
            print ('An Error Occurred Creating the Entry');
        }

        // get new entry_id
        $entry_id = $this->EE->api_channel_entries->entry_id;  
        $this->EE->api_channel_entries->entry_id = NULL;  

        $query =$this->EE->db->query( "UPDATE exp_channel_data
                                       SET field_id_$field_id = 1
                                       WHERE entry_id = $entry_id
                                     " );

        // link matrix record to new entry
        $query =$this->EE->db->query( "UPDATE exp_matrix_data
                                       SET entry_id = $entry_id
                                       WHERE entry_id = $field->entry_id
                                     " );

       $entry_rel[$field->entry_id] = $entry_id; 
    }

    // link field to new group
    $query =$this->EE->db->query( "UPDATE exp_channel_fields
                                       SET group_id = $new_field_group
                                       WHERE field_id = $field_id
                                     " );
    // do playa rel.
    foreach ($entry_rel as $old_entry => $new_entry)
    {
            $data_rel = array (
                        'parent_entry_id' => $old_entry
                        'parent_field_id' => $playa_field_id
                        'child_entry_id' => $new_entry
            );

            $query =$this->EE->db->query( "UPDATE exp_channel_data
                                       SET field_id_$playa_field_id = "[$new_entry][my new  entry with matrix][my_new_matrix_url_title]"
                                       WHERE entry_id = $old_entry
                                     ");

            $this->EE->db->insert('exp_playa_relationships', $data_rel);
    }
}   
?>
2
  • take attention, that you still need decide what you will choose as new entries names.
    – Max Lazar
    Feb 4, 2014 at 11:07
  • 1
    note - you need to run this code from EE template
    – Max Lazar
    Feb 4, 2014 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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