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?
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.