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

ExpressionEngine Logo ExpressionEngine
Features Pro new 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

Save relationship and grid fields with the Channel Entry Model

Development and Programming

Antwanvb's avatar
Antwanvb
6 posts
about 2 months ago
Antwanvb's avatar Antwanvb

I’m trying to create an entry with the Channel Entry Model and come as far as the example shows.

TLDR; The entry that i want to populate also has Grid and Relationship fields that i want to store data inside, how do i do this?

The way i’m approaching this now.

I create an entry via the control panel and see which data is stored where. I then found that in need to save some id’s (for relationships) in the exp_relationship and (for grid) the exp_grid_columns table. those id’s then point to the entries where i need to relate to.I understood the way grid stores it’s data, but i’m not seeing on how i go about and store relationships.

I’m thinking of saving the entry first then i get the entry id saved and then i can populate the exp_relationship or exp_grid_columns with the id’s necessary.

$entry->entry_id; // Will now return the new Entry ID. Store grid/relationship data

Now that i have the entry id that is saved i can populate the columns necessary to make the relations.

Does anyone have a simpler solution? like a method from EE that i am unaware of or should i just carry on with this approach.

       
Antwanvb's avatar
Antwanvb
6 posts
about 2 months ago
Antwanvb's avatar Antwanvb

So i kinda found out how it works! Not the actual code i’m using but i stripped it down and simplified it and hopefully made it more readable.

This way you’ll at least have another example as shown from the docs on how to create an entry with the ChannelEntry model.

Maybe someone else has a use for it.

Use this power with care:

public function createEntry(){

    // Channel id of which to create an entry from
    $channel_id = 2;
    // Title is used for the title field and url field
    $title = 'Entry Title 15';
   
    // Some basic entry stuff
    $entry = ee('Model')->make('ChannelEntry');
    $entry->channel_id = $channel_id;
    $entry->site_id =  ee()->config->item('site_id');
    $entry->author_id = ee()->session->userdata('member_id');
    $entry->ip_address = ee()->session->userdata('ip_address');
    $entry->status = "open";
    $entry->sticky = FALSE;
    $entry->title = $title;
    $entry->url_title = ee('Format')->make('Text', $title)->urlSlug()->compile();
    $entry->entry_date = ee()->localize->now;
    $entry->edit_date = ee()->localize->now;

    // Now the good stuff
    // -------------------------------
    // Save a custom fields
    $text_input_field_id = 2; // This is the id of the custom text fieldtype
    $entry->{'field_id_' . $text_input_field_id} = 'Custom text field!';
    
    // Save a relationship field
    $relationship_field_id = 8; // This is the id of the custom relationship fieldtype
    // Array of entry_id's to relate to in the relationship field multiple would be [1,2]
    $ids = [1];
    $entry->{'field_id_'. $relationship_field_id} = array('data' => $ids);
    
    // Save a Grid field
    // This is the id of the custom grid fieldtype
    $grid_field_id = 9;
    // I have 3 fields inside the grid: a relationship, number and text field
    // The col_id's for the fieldtype can be found inside the grid_columns table
    $grid_values = [
      'col_id_7' => array('data' => $ids),
      'col_id_8' => 9001,
      'col_id_9' => 'Custom Field is set!'
    ]; 
    // To save a new grid row you use new_row_{ROW_ID}
    $entry->{'field_id_' . $grid_field_id} = array(
      'new_row_0' => $grid_values, 
      'new_row_1' => $grid_values
    );
    // -------------------------------

    // Validate and Save.
    $result = $entry->validate();

    if ($result->isValid()) {
      // Valid-> Save the entry
      $entry->save();
    } else {
      // Probably invalid
    }
  }

I found part of the answer from this post’s answer by Harsh Barach.

       
darthvader's avatar
darthvader
1 posts
about 2 months ago
darthvader's avatar darthvader

I’ll save the record first, then get it back with the ID saved. I can then populate some of these fields (ex: exp_relationships and exp_grid_columns) with the necessary ID’s.

       
Blair L's avatar
Blair L
110 posts
6 days ago
Blair L's avatar Blair L

Did you ever get this working with editing relationships on existing entries? Working on improving docs here…

       
Antwanvb's avatar
Antwanvb
6 posts
one day ago
Antwanvb's avatar Antwanvb

Don’t have the time now to test it so maybe later i’ll confirm but it’ll be somewhere along the line of:

// Get the entry object.
$entry_id = 3;
$entry = ee('Model')->get('ChannelEntry', $entry_id)->first();

// Save a relationship field
$relationship_field_id = 8; // This is the id of the custom relationship fieldtype
// Array of entry_id's to relate to in the relationship field multiple would be [1,2]
// the index of the array would probably determine the order but i haven't tested it
$ids = [1,2,3,4,5,6];
$entry->{'field_id_'. $relationship_field_id} = array('data' => $ids);

// Validate and Save.
$result = $entry->validate();

if ($result->isValid())
{
  $entry->save();
}

As somewhat explained in the docs

Hope it helps

       
Blair L's avatar
Blair L
110 posts
one day ago
Blair L's avatar Blair L

Thanks! I have been working on some updates to the Model documentation, so the current entry doc examples were me not quite getting through those more complex field types the first time. Creating a new entry with relationships worked, but couldn’t quite get editing to work. Wanted to see if it was my attempt or an issue with EE.

       
Antwanvb's avatar
Antwanvb
6 posts
one day ago
Antwanvb's avatar Antwanvb

I’ve experienced the same struggle so I for one very much appreciatie you take your time improving the docs for addon development.

I’ve not yet fully grasped it yet since i only required it to create an entry. I’ll make sure to share what i’ve found if i ever get to it. 😊

       

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.