Good Evening,
I’m in the midst of moving a website over from an old Code Igniter version to a new Expression Engine instance. Part of the migration is moving over all the data.
One of the fields in my EE channel ‘race’ is ‘registration_opens’. In the old world this was a string, but in the new world I’m leveraging the time select field type within EE. I’ve written a function that will provide me the number of seconds for the time, which I have verified is working correctly, but whenever I try to insert via the ‘Create Entry API’ a different value seems to be getting saved (I keep seeing 7620).
Here is my current code:
<?php
$this->EE->load->library('api');
$this->EE->api->instantiate('channel_entries');
$this->EE->api->instantiate('channel_fields');
$results = $this->EE->db->query("SELECT * FROM runs ORDER BY run_id ASC");
$time = time();
if ($results->num_rows() > 0)
{
foreach($results->result_array() as $row)
{
$online_reg = ($row['allow_online_registration'] == 1) ? "Yes": "No";
$add_fee = ($row['add_fee'] == 1) ? "Yes": "No";
$volunteers = ($row['allow_volunteers'] == "Y") ? "Yes": "No";
$target_time = ($row['allow_target_time'] == "Y") ? "Yes": "No";
$pace = ($row['allow_projected_pace'] == "Y") ? "Yes": "No";
$coupons = ($row['allow_coupons'] == "Y") ? "Yes": "No";
$state = convert_state($row['state'], $to='name');
$reg_opens = format_time($row['registration_opens']);
echo("Value returned: ($reg_opens)");
$data = array(
'channel_id' => 7,
'title' => ''.$row["name"].'',
'entry_date' => ''.$time.'',
'field_id_9' => ''.$row["run_date"].'',
'field_ft_9' => 'none',
'field_id_10' => ''.$row["web_site"].'',
'field_ft_10' => 'none',
'field_id_11' => ''.$row["race_logo"].'',
'field_ft_11' => 'none',
'field_id_12' => ''.$row["company_logo"].'',
'field_ft_12' => 'none',
'field_id_13' => ''.$state.'',
'field_ft_13' => 'none',
'field_id_15' => ''.$reg_opens.'',
'field_ft_15' => 'xhtml',
'field_id_16' => ''.$row["run_time"].'',
'field_ft_16' => 'xhtml',
'field_id_17' => ''.$row["description"].'',
'field_ft_17' => 'none',
'field_id_18' => ''.$row["contact_name"].'',
'field_ft_18' => 'none',
'field_id_19' => ''.$row["contact_email"].'',
'field_ft_19' => 'none',
'field_id_20' => ''.$row["contact_phone"].'',
'field_ft_20' => 'none',
'field_id_21' => ''.$online_reg.'',
'field_ft_21' => 'none',
'field_id_22' => ''.$row["online_registration_time"].'',
'field_ft_22' => 'none',
'field_id_23' => ''.$row["online_registration_deadline"].'',
'field_ft_23' => 'none',
'field_id_25' => ''.$add_fee.'',
'field_ft_25' => 'none',
'field_id_26' => ''.$row["commission"].'',
'field_ft_26' => 'none',
'field_id_27' => ''.$pace.'',
'field_ft_27' => 'none',
'field_id_28' => ''.$coupons.'',
'field_ft_28' => 'none',
'field_id_29' => ''.$volunteers.'',
'field_ft_29' => 'none',
'field_id_30' => ''.$row["payee"].'',
'field_ft_30' => 'none',
'field_id_31' => ''.$row["payee_address"].'',
'field_ft_31' => 'none',
'field_id_129' => ''.$target_time.'',
'field_ft_129' => 'none',
'field_id_38' => ''.$row["max_registration"].'',
'field_ft_38' => 'none',
'field_id_128' => ''.$row["run_id"].'',
'field_ft_128' => 'none',
'field_id_130' => ''.$row["user_id"].'',
'field_ft_130' => 'none'
);
$this->EE->api_channel_fields->setup_entry_settings(7, $data);
if ($this->EE->api_channel_entries->submit_new_entry(7, $data) === FALSE)
{
show_error('An Error Occurred Creating the Entry');
}
else{
echo('Insert successful!<br>');
}
break;
}
}
function format_time($time_passed){
$time_passed = strtoupper($time_passed);
$time_passed= preg_replace('/\s+/', '', $time_passed);
$time_passed= str_replace('.', '', $time_passed);
$pieces = array();
$second_pieces = array();
$pos = strpos($time_passed, "PM");
if ($pos === false) {
$time_base = 0;
$time_bucket = "AM";
} else {
$time_base = 40200;
$time_bucket = "PM";
}
$pieces = explode(":", $time_passed);
$first_part = $pieces[0]; // piece1
$time_base = $time_base + (intval($first_part) * 3600);
echo("Time base after the first section ($first_part - $time_bucket) reviewed: ".$time_base."<br>");
$second_pieces = explode($time_bucket, $pieces[1]);
$second_part = $second_pieces[0];
$time_base = $time_base + (intval($second_part) * 60);
echo("Time base after the second section ($second_part - $time_bucket) reviewed: ".$time_base."<br>");
return $time_base;
}The echos I have will display the appropriate amount of seconds, like 25200, but then isn’t updated in the DB appropriately.
Any idea what I’m missing?
Thanks, Matt
I would imagine it’s this third party fieldtype that could be causing the issue. If this is a one-time import could you change the fieldtype to a text input, import all the data and then switch the field to a time-select fieldtype. You wouldn’t lose any data switching fieldtypes like that and so long as the value in the database is correct it should work.
Also change the ‘field_ft_15’ to ‘none’ if this is the corresponding field for the timeselect field
It’s odd though…I can see the database field is of type ‘text’ regardless of whether or not it’s the time select field type, which is why I am not sure what’s going on.
I have the type as xhtml because the other entries through the insert form are entered like that, so I wanted to keep it consistent.
To debug I would definitely run a test as I suggest above on one entry. The issue isn’t the database it’s the pre-formatting that is going on before the data is inserted. XHTML shouldn’t make any real difference to the field but isn’t needed but if I suspect it works as you expect on a text field you will have to dig into the time-select fieldtype code to see what pre-formatting it is doing prior to the data being inserted and what inputs it expects.
Very interesting - when I changed the field type to text field it saved it appropriately via the API.
My apologies for not trying this on the first recommendation, I didn’t think there would be any field type intersection with the direct API call. It must have been something with the time select type expecting a different format as you mentioned.
I suppose in the short term when I’m moving the items over, I’ll have to have it as text and then I can convert it.
Thank you very much for the help!
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.