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

member field inside grid field, via add-on (entry model; grid model)

Development and Programming

directordash's avatar
directordash
14 posts
one year ago
directordash's avatar directordash

Here is my function:

private function create_user_rows($entry, $user_count, $input_id)
 {
     
     $grid_field_id = 20;
     $default_member_id = 123; 
 
     $new_grid_data = [];
 
     for ($i = 0; $i < $user_count; $i++) {
         if ($i == 0) {
             // For the first row, use input_id and fetch the username
             $member_username = ee('Model')->get('Member', $input_id)->first()->username;
    
             $new_grid_data['new_row_' . $i] = [
                 'col_id_58' => $member_username, // Populate with the username of the author
                 'col_id_59' => $input_id,       // Set the relationship field to the author_id
                 'col_id_62' => 'unassigned',    
                 'col_id_60' => 0,          
                 'col_id_61' => 0                 
             ];
         } else {
             // For subsequent rows, use the default member_id
             $new_grid_data['new_row_' . $i] = [
                 'col_id_58' => '',               // No username
                 'col_id_59' => $default_member_id,   // Default member ID for relationship field
                 'col_id_62' => 'unassigned',   
                 'col_id_60' => 0,          
                 'col_id_61' => 0   
             ];
         }
     }
 
     // Load grid model to save grid data
     ee()->load->model('grid_model');
 
     // Save the grid data to the entry using the recommended model-based method
     $entry->{$entry->getCustomFieldPrefix() . $grid_field_id} = $new_grid_data;
     $entry->save();
 
     // Double-save with the grid model to ensure grid data is committed
     ee()->grid_model->save_field_data($new_grid_data, $grid_field_id, 'channel', $entry->entry_id);
 }

col_id_59 is a member relationship field. I know that the member rel field type is typically looking for a ‘data’ array, but I feel like I’ve tried that and everything else at this point. All the other columns populate as expected; just not that one.

What am I missing?

       
creativearc's avatar
creativearc
68 posts
one year ago
creativearc's avatar creativearc

You might try asking this one in the Slack #programming channel.

https://expressionengine.com/blog/join-slack

       
lowalax45's avatar
lowalax45
1 posts
10 months ago
lowalax45's avatar lowalax45

Thanks for your answer flash games. It is useful for me.

       
Rosariokris's avatar
Rosariokris
2 posts
one month ago
Rosariokris's avatar Rosariokris

I have been waiting for this answer for a long time papa’s freezeria

       

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.