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

Adding records with database class - a mystery

Development and Programming

jejuna's avatar
jejuna
105 posts
6 years ago
jejuna's avatar jejuna

I’m creating and inserting some entries using the database class. One is the title, the other is the data. I’m able to add these to the database with no errors. I can look in phpMyAdmin and I see them. I’ve compared them to ones I’ve added through the control panel, and they are identical.

I can’t see them in control panel when I go into Edit->Entries. I can’t output them using the channel:entries tag. HOWEVER, they output perfectly using the exp:query tag. I’m using the data in the just-created record to make a PDF on the fly. That is working!

Why can’t I see the entries in the back end? I’m using EE 5.1.2. Any ideas or advice is appreciated.

       
Gareth Davies's avatar
Gareth Davies
491 posts
6 years ago
Gareth Davies's avatar Gareth Davies

You should check out the model service https://docs.expressionengine.com/latest/development/services/model.html

ExpressionEngine has a complex set of data types, such as Channels, Members, Templates, and Categories. These have properties and constraints, as well as relationships and complex storage requirements.

As per this section of those docs my guess would be that you are missing a relationship or similar.

       
jejuna's avatar
jejuna
105 posts
6 years ago
jejuna's avatar jejuna

Thanks for replying. Sadly, I couldn’t find any examples or info on using Models to insert channel entries in the documentation. I did find a legacy API on channel entries that seems to make it easier. But again, sadly, it thinks that all the custom entry data is going to live in exp_channel_data, and EE doesn’t work like that anymore. I was able to modify the code to plonk the data into the one table I’m using. It does work, but I still didn’t see the entry in the control panel.

But then, I was looking around in the database more, and I noticed that exp_channel_data is still there. It seems like that’s the ‘relationship’ I was missing. I put the entry_id and channel_id in there, and voila! All good!

If there is indeed an easier way using Models or some other such thing, it would be good to know about along some examples and documentation would be quite helpful.

       
Gareth Davies's avatar
Gareth Davies
491 posts
6 years ago
Gareth Davies's avatar Gareth Davies

Here’s a basic example that you can edit to create a new entry:

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

$entry->set(array(
 'title' => 'I am a title',
 'url_title' => 'i_am_a_title',
 'channel_id' => 1,
 'status' => 'open',
 'entry_date' => time(),
 'author_id' => 1,
 'field_id_1' => "I am a custom field"
));

$entry->field_id_2 = "Can add like this too";

$entry->save();
       
jejuna's avatar
jejuna
105 posts
6 years ago
jejuna's avatar jejuna

Gareth, thanks so much for your help on this. What you’ve described IS a lot easier. However, it doesn’t work! What I’m doing is creating a coupon code in Cartthrob on the fly. I get the title, amount off, etc, from an entry in my orders channel right after the order is submitted.

In Cartthrob, the details about the coupon code are serialized and encoded. So I do that with php. That all works fine. But using the Model, it doesn’t insert it into my custom field. This does work when I do it manually using the database class to do an insert into channel_titles, channel_data, and channel_data_field_90, which is my custom field.

Here’s my code:

<?php
$trans_id = '3333';
$amount_off = 100;
$coupon_data = array(
'type'   => 'Cartthrob_discount_amount_off',
'amount_off' => $amount_off,
'used_by'  =>'',
'per_user_limit'  => '1',
'discount_limit'  => '1',
'member_groups' =>'',
'member_ids'   =>''
); 
$data_string = serialize($coupon_data); 
$coupon_array_encode = base64_encode($data_string); 
$entry = ee('Model')->make('ChannelEntry');
$entry->set(array(
 'title' => 'GCdd'.$trans_id,
 'url_title' => 'gcdd'.$trans_id,
 'channel_id' => 8,
 'status' => 'open',
 'entry_date' => time(),
 'author_id' => 1,
 'field_id_90'=> $coupon_array_encode
 ));
$entry->save();
?>

I’ve tried it outside of the array, like you have in the example:

$entry->field_id_90 = $coupon_array_encode;

but, nope.

Oddly, if I try this same thing in another channel with a different custom field, it works. I also tried making a new custom field and channel, just for kicks, but also, nope. I’m stumped.

       
Gareth Davies's avatar
Gareth Davies
491 posts
6 years ago
Gareth Davies's avatar Gareth Davies

I’m not well versed in Cartthrob but the fact you say this works in a different custom field makes me think something else is going on. When $entry->save() is called it’s calling anything that’s hooked into it. Do you have any extensions that might be overwriting the data you have put into field_id_90? You could test that by disabling any extensions temporarily and seeing if it saves.

       
jejuna's avatar
jejuna
105 posts
6 years ago
jejuna's avatar jejuna

I have no extensions installed, and my old-timey manual insertion in the three places is wonky, I know, but it does work. That’s why I don’t think Cartthrob itself is doing anything to overwrite anything. And that’s why I created a new channel and new table, just in case something got corrupted with all my inserting and manual deleting.

I have to move on from this to complete my project but I’ll continue to futz around. It seems to be a mystery, indeed. I appreciate the help and the follow-up. Many thanks.

       

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.