Jump to content

Problem creating new page from API with custom field with a float subfield


adrian
 Share

Recommended Posts

Hi everyone,

Here's something I have been battling with.

I am trying to create a new page via the API with a template that contains a MapMarker field. The template also contains two other custom field types (phone and dimensions, which are both fine). The problem comes down to the fact that the lat and lng subfields of the MapMarker fieldtype are floats.

So, when you try to save the page, you get the following error:

Warning: 1265 Data truncated for column 'lat' at row 1

which makes complete sense. Here is the generated SQL:

INSERT INTO `field_map` (pages_id,data,lat,lng,status,zoom) VALUES('1494','','','','0','0')

It's getting the zeros correct for status and zoom, which are integer fields.

So, the way around it is to set those subfields to zero before saving. So, this works:

$basic_page = $templates->get("basic-page");

$wp = new Page();
$wp->parent = $pages->get("/about/");
$wp->template = $basic_page;
$wp->title = 'testme';

foreach($wp->fields as $pf){
    if($pf->type=="FieldtypeMapMarker"){
        $wp->$pf->lat = 0;
        $wp->$pf->lng = 0;
    }
}
$wp->save();
 

But I want to come up with a solution that will allow creating a page with any custom field that might use float sub fields.

I even thought about setting the values of all fields and subfields to 0 before (to be overidden later), but can't even do that because it seems impossible to find out the subfield names until a page is created with that template - am I missing something obvious here?

Then again, I feel like this is PW should handle properly in the core. I am not even sure I know why it is trying to set the values of fields on page save if they weren't specified anywhere.

Any thoughts?

EDIT: I am beginning to think this is actually an issue in general with MapMarker as I can't create new pages in the admin either - I'll investigate further.

Edited by adrian
Link to comment
Share on other sites

Hey Wanze,

You're right for sure.

I got thrown because I though it was just API related, but it actually seems like a bug between MapMarker and the latest version of PW. I looked back at a couple of sites that are working fine and MapMarker has always defined those as '' and not 0 and they have always been float fields. Not exactly sure what changed where, but this definitely fixes it.

I'll submit a pull request for Ryan.

Thank You!!

  • Like 1
Link to comment
Share on other sites

You're welcome.

I think Ryan did some work on the float fields - there was a problem that float values were "cut down" to integers due to the local set by PHP, if multilang support was enabled. Maybe something changed how an empty value is handled too.

Cheers

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...