2

I can't figure out how to save (or retrieve) relationship field data using the Model Service. I can save flat fields with out an issue (and retrieve them).

So when retrieving an entry like this:

$entry = ee('Model')
        ->get('ChannelEntry')
        ->filter('channel_id', 1)
        ->first();

var_dump($entry->field_id_1); // this is a text field data, this works

var_dump($entry->field_id_2); // this is a relationship field with data, it outputs NULL

As you can see, I don't get the relationship data. I couldn't figure out a way to load relationship data either, by using ->with("Relationships")or any other verbiage that makes sense.

At the same time, I can't seem to save relationship data in any way:

$entry = ee('Model')->make('ChannelEntry');

$entry->title = "Title";
$entry->url_title = "title";
$entry->site_id = 1;
$entry->channel_id = 1;
$entry->status = "open";
$entry->author_id = 1;
$entry->field_id_1 = "My text field"; // this works
$entry->field_id_2 = array("100", "101"); // doesn't save relatoinship data, and trying a few different formats didn't work
$entry->save();

I've been digging through the EE source code and it isn't clear if this is possible at all. I know I could insert raw DB entries next to the Model Service, but that sucks and is harder to get right.

Any one have any success with this?

2 Answers 2

2

I too had the same issue, the EE documentation to put it nicely is rather incomplete on this topic. Looking though the source I saw an example in ft.relationships.php:

ee('Model')->get('ChannelEntry', $entry_id)
                    ->with('Children', 'Channel')
                    ->fields('Channel.channel_title', 'Children.entry_id', 'Children.title', 'Children.channel_id')
                    ->first()
                    ->Children;  

This will give you just the child relationship entries, so you could foreach your entries to get each entry's children. Not very satisfactory!

1
  • Wow. Thank you for your help! But Not very satisfactory! is crazy accurate. I was more importantly looking to figure out how to save a new entry with a relationship field, and there is no example of that in the ft.relationships.php file, so maybe that isn't possible, as it looks like saving a relationship field is all db inserts. I ended up just using a batch_insert(), but that didn't make a very happy developer. Thanks again.
    – jrothafer
    Commented Apr 29, 2019 at 5:59
1

Yeah, you can save relationship data using below Model query. In below code $entry->field_id_2 indicates the relationship data.

$entry = ee('Model')->make('ChannelEntry');
$entry->channel_id = 'your_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 = 'your_entry_title';
$entry->url_title = preg_replace("/[\s_]/", "-", 'your_entry_url_title');
$entry->field_id_2 = array('data'=>array("100", "101")); //ids of related entry
$entry->entry_date = ee()->localize->now;
$entry->edit_date = ee()->localize->now;
$entry->save();

Well, as far as I know there is no any method to fetch relationship data using Model. Because, as you should know relationship is third-party Addon. But you can get data of relationship from exp_relationships using below simple query.

$entry = ee()->db->get_where('exp_relationships', array('parent_id'=>'your_parent_entry_id'));
var_dump($entry->result());

I hope this will helps you.

1
  • 1
    I appreciate the answer, but the Relationships field is a first party-addon that ships with EE2/3/4/5. github.com/ExpressionEngine/ExpressionEngine/tree/stability/… But that is really good information about how to save data to a relationship field in an EE Entry Model, although I haven't tested your answer. The answer I accepted shows how to retrieve it from an EE Entry Model. I wish I could give you both the points.
    – jrothafer
    Commented Apr 29, 2019 at 7:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.