4

I need to use the channel entries API to save a relationship between two channels. I create an entry to channel A and then I create an entry to Channel B, channel B has a relationship field that needs to be set to point to the entry on Channel A.

I can create the entries with the save_entry() function, but I can't decipher how to set the relationship field on Channel B to Channel A.

My process is:

when I create the entry on Channel A I use the entry ID and pass it as the value of Channel B's relationship field. This does not work.

 $channel_b_entry = array(
        'title' => $data['title'],
        'field_id_26' => $data['type'],
        'field_id_29' => $data['channel_a_entry_id'],
        'field_ft_29' => 'xhtml',            
    );

In this case field_id_29 is a relationship field to channel A entries.

I have tried passing the id as an array

 $channel_b_entry = array(
        'title' => $data['title'],
        'field_id_26' => $data['type'],
        'field_id_29' => array($data['channel_a_entry_id']),
        'field_ft_29' => 'xhtml',            
    );

But that didn't work either.

Do I need to setup the relationship my self to the exp_relationship table, or the save_entry() takes care of that?

3

I've managed to tackle this issue myself using the following. The key data structure is immediately below with a full example beneath that. This will handle all the relationship stuff automatically.

Data array structure for relationships

$data = array(
    'title' => $title,
    'entry_date' => $date,
    'edit_date' => $date,
    'field_id_29' => array( 
        'data' => array( 
            $id
        ) 
    ),
    'field_ft_29' => 'xhtml'
);

Full example

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

// Set up data for Channel A
$channel_a_id = 1;
$date = time();
$title = "Channel A Title";

$channel_a_data = array(
    'title' => $title,
    'entry_date' => $date,
    'edit_date' => $date
);

// Setup and save channel a entry
ee()->api_channel_fields->setup_entry_settings( $channel_a_id, $channel_a_data );
$success = ee()->api_channel_entries->save_entry( $channel_a_data, $channel_a_id );

// If successful move onto creating channel b entry
if ( $success ) {

    // Get the newly created entry_id from channel a
    $channel_a_entry_id = ee()->api_channel_entries->entry_id;
    $channel_b_id = 2;
    $title = "Channel B Title";

    $channel_b_data = array(
        'title' => $title,
        'entry_date' => $date,
        'edit_date' => $date,
        'field_id_29' => array( 
            'data' => array( 
                $channel_a_entry_id
            ) 
        ),
        'field_ft_29' => 'xhtml'
    );

    // Setup and save channel b entry
    ee()->api_channel_fields->setup_entry_settings( $channel_b_id, $channel_b_data );
    ee()->api_channel_entries->save_entry( $channel_b_data, $channel_b_id );

}
| improve this answer | |
0

I got this working by inspecting the publish page, and a relationship field has a 'name' that looks like this:

<select name="field_id_90[data][]">

So it looks like EE expects a certain format to be submitted for relationships.

So you could add to your existing array like so:

$channel_b_entry['field_id_29']['data'][] = $data['channel_a_entry_id'];
| improve this answer | |

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.