I am having trouble saving a ChannelEntry with a relationship to another ChannelEntry using the Models Service.

The following produces no errors, but a row in exp_relationships with no field_id

$entry = ee('Model')->get('ChannelEntry')
  ->with('Children', 'Channel')
  ->filter('entry_id', 330)->first();

$child = ee('Model')->get('ChannelEntry')->filter('channel_id', 7)->filter('title', 'My title')->first();

$entry->Children[] = $child;

In the Control Panel, this shows no relationships have been set. I can manually add the field_id to the DB, and poof the relationship is created.

So how do I create the relationship with the field_id column populated?

Also, I noticed it also creates a row in the exp_channel_data_field_23 (the id of my relationship field) with a NULL value in the field_id_23 column. Adding a relationship through the Control Panel does not produce a row in this table at all, however...


Solution was to set an array with a 'data' property to the field. So from my example:

$entry->field_id_23 = ['data' => [$child->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.