Hello,
I’m currently working on some external EE module built with CodeIgniter. One of the thing I need to do is to import some complex XML into a custom weblog with relationships.
I do this in 3 step : - First insert data into exp_weblog_titles - Then insert data into exp_relationships - Then insert data into exp_weblog_data
Here’s my code :
// Insert data into exp_weblog_titles
$title_data[$key] = array(
'entry_id' => '',
'weblog_id' => $weblog_id,
'author_id' => $author_id,
'site_id' => $site_id,
'ip_address' => $ip_adress,
'title' => $title,
'url_title' => $url_title,
'entry_date' => $date,
'dst_enabled' => 'n',
'edit_date' => date("YmdHis"),
'versioning_enabled' => 'n',
'year' => date('Y'),
'month' => date('m'),
'day' => date('d'),
'expiration_date' => $expire,
'comment_expiration_date' => 0,
'sticky' => 'n',
'status' => $status,
'allow_comments' => 'n',
'allow_trackbacks' => 'n',
'forum_topic_id' => 0
);
$this->db->insert('_weblog_titles', $title_data[$key]);
$title_entry_id = $this->db->insert_id();
// Insert data into exp_relationships
$relation_data[$key] = array(
'rel_id' => '',
'rel_parent_id' => $title_entry_id,
'rel_child_id' => $child,
'rel_type' => 'blog'
);
$this->db->insert('_relationships', $relation_data[$key]);
$relation_entry_id = $this->db->insert_id();
// Insert custom data
$custom_data[$key] = array (
'entry_id' => $title_entry_id,
'site_id' => $site_id,
'weblog_id' => $weblog_id
);
// Set child field
$custom_data[$key]['field_id_32'] = $child;
$this->db->insert('_weblog_data', $custom_data[$key]);
Everything work well, data stored in the database seems good (I’ve looked at some var dump). But For a few entry, the relation doesn’t appear correct in the edit panel : relation selected option isn’t the good one !
When editing I change it from the edit panel to the good one, it still don’t work. But when I change to another one, then save, then edit and change it again but to the good one, it works !
I know my question is a bit difficult to understand, but all help would be appreciated because I’m really stuck right now !
Thanks in advance !
Regards,
I’ve found the solution, post it there, it may help :
Here’s some nice wiki article about inserting relationship manually : http://expressionengine.com/wiki/Relationships_-_Under_the_Hood/
My error was to insert child id into exp_weblog_data custom field, instead of auto-generated rel_id :
// Error was : $custom_data[$key]['field_id_32'] = $child;
// Must insert auto-generated rel_id
$custom_data[$key]['field_id_32'] = $relation_entry_id;
Sorry for disturbing 😊
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.