EllisLab text mark
Advanced Search
     
API Help: Creating entry with custom field using PHP
Posted: 03 August 2012 06:31 AM   [ Ignore ]
Avatar
Joined: 2010-02-07
18 posts

Hello,

I’m trying to create a little script that will add an entry using PHP. This is a simple task until I have a custom field with a third-party field type, in this case a Cartthrob package field. The script I’ve written below will create the entry, but leaves the custom field empty.

Can you help me work out how to add content to this custom field using PHP?

Many thanks,


Mike

<?php

$channel_id 
14;
$package_products = array ( => array ( 'entry_id' => '36''description' => '', ), => array ( 'entry_id' => '37''description' => '', ), );

$this->EE->load->library('api');
$this->EE->api->instantiate('channel_entries');
$this->EE->api->instantiate('channel_fields');

$data = array(
        
'title'         => 'PHP Added an Entry!',
        
'entry_date'    => '1256953732',
        
'field_id_96' => base64_encode(serialize($package_products)),
        
'field_ft_96'    => 'none'
);

$this->EE->api_channel_fields->setup_entry_settings($channel_id$data);

if (
$this->EE->api_channel_entries->submit_new_entry($channel_id$data) === FALSE)
{
        show_error
('An Error Occurred Creating the Entry');

?> 

[Mod Edit: Moved to the Development and Programming forum]

 
Posted: 03 August 2012 07:37 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2005-12-21
198 posts

Hi Mike,

It’s unlikely that you have to base_64_encode and serialize the data before adding it - I expect the CartThrob fieldtype will do this.

The field_id_96 array element needs to be the value that the Publish form sends. The fieldtype will then use that value and build the correct format data to add to the database (almost certainly serializing it at this point).

You can check what the value should be by editing the file:

system/expressionengine/libraries/api/Api_channel_entries.php 

and adding:

print_r$data ); exit; 

to the submit_new_entry() function. Then when you add an entry from the Control Panel you can see what value the array expects to receive.

(Remove the print_r afterwards…)

Andrew

 Signature 

Andrew Weaver — brandnewbox.co.uk  — @ajweaver


Member of EE Professional Network · More plugins, extensions and modules · Documentation

 
Posted: 03 August 2012 07:57 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2010-02-07
18 posts

Thank you, that was the answer - the data didn’t need to be serialised or encoded.