Insert records into pods using php
-
Hello Guys,
I have an plugin where I import some data, and then create an php array.
This array is already on the same specs as one pod I created.How do I insert into my pod these records?
-
@shoptutor – we resolved this issue in our Slack right?
For anyone googleing into this, the answer was Pods::save()
Yep, it worked here,
I’m just not sure how to make it work the check for existing records.
What I got so far is:
$pod_data_array = array( 'name' => $record_title, 'productitemid' => $record_productitemid, //This is an unique field 'productitem_name' => $record_productitem_name, 'date_modified_text' => $record_date_modified, 'date_modified' => $record_date_modified, 'description' => $record_description, 'descriptionlong' => $record_descriptionlong, 'manufacturer' => $record_manufacturer, 'ean' => $record_ean //This is also an unique field ); $mypod = pods( 'ofertas' ); //Before Save I should check if there is an record inside my pod, with the same 'ean' as this new record I got here. $mypod_id = $mypod->save( $pod_data_array ); echo 'Record no' . $record_queue_number . 'saved!';
Easier to explain with inline comments in code. This would need to have the
$pod_data_array
and all the variables making it up before it.//query by unique ID to see if a record with this unique ID exists $params = array( 'where' => 'productitemid.meta_value = "'.$record_productitemid.'"', ); //start $id null $id = null; $mypod = pods( 'ofertas', $params ); //check if we have results if ( 0 > $mypod->total() ) { //if so, should only be one result since we are querying by a unique ID, so loop should run one //set $id to the result we found $id = $pods->id(); } //reset iterator $mypod->reset(); //save using $id to update existing, or if it's still null create new $mypod->save( $data, null, $id );
Awesomee!!!
Thanks.
Hello, I’m back on this…
Sometimes, after several loops saving data, It is crashing during inserts, so I had performed some tests and figured that it is crashing during the:
$mypod->save( $data, null, $id );
Reading the pod’s save page I read the following.
“IMPORTANT: This example skips data sanitization and validation for the sake of simplicity to explain a concept. Do not use as is, always sanitize data from a form.“
So I thought that may be something in the data.
What kind of sanitization do you talk about there?
where I can find more info about it?Another thing is that it never crashes on the fists saving loops. Always after inserting 200 and something…
Any ideas?
I sanitized everything, and had just compared the data from the last pod saved with the one that made the error, and there is no diference.
So I guess its not a data sanitization issue.
any other ideas?
Is it possible that it is crashing at save becouse it is getting to much data?
It creashes always near the row 200, never before 150.
The data I’m inserting is something like this:array(39) { ["name"]=> string(12) "Mini systems" ["description"]=> string(70) "Mini System LG - CM2730 160W, Bluetooth, USB, MP3, Cd e Rádio - Preto" ["comment_status"]=> string(4) "open" ["ping_status"]=> string(6) "closed" ["post_status"]=> string(7) "publish" ["post_content"]=> string(70) "Mini System LG - CM2730 160W, Bluetooth, USB, MP3, Cd e Rádio - Preto" ["post_excerpt"]=> string(12) "Mini systems" ["post_category"]=> string(2) "TV" ["category"]=> string(2) "TV" ["post_name"]=> string(12) "Mini systems" ["post_title"]=> string(12) "Mini systems" ["post_author"]=> int(2) ["post_date"]=> string(25) "2015-02-23T08:53:38+01:00" ["latest_update"]=> string(19) "2015-02-25 02:24:48" ["session"]=> string(2) "TV" ["saleavailability"]=> string(1) "1" ["featureavailability"]=> string(1) "0" ["sessioncheck"]=> string(1) "0" ["expected_margin"]=> int(49) ["affiliatesystem"]=> string(5) "Zanox" ["productitemid"]=> string(32) "cc36e3a5fe178746637124c5936639fb" ["productitem_name"]=> string(12) "Mini systems" ["date_modified_text"]=> string(25) "2015-02-23T08:53:38+01:00" ["date_modified"]=> string(25) "2015-02-23T08:53:38+01:00" ["idprogram"]=> int(12011) ["program_name"]=> string(7) "Walmart" ["price"]=> int(899) ["currency"]=> string(3) "BRL" ["trackinglink_adspaceid"]=> int(2034022) ["trackinglink_ppv"]=> string(218) "https://ad.zanox.com/ppv/?30105432C89279541&ULP=Eletronicos/Mini-systems/LG/415012-Mini-System-LG-CM2730-160W-Preto?utm_source=zanox&utm_medium=xml_Zanox&utm_campaign=Zanox&zpar9=4209ECD457683232DBD9" ["trackinglink_ppc"]=> string(218) "https://ad.zanox.com/ppc/?30105432C89279541&ULP=Eletronicos/Mini-systems/LG/415012-Mini-System-LG-CM2730-160W-Preto?utm_source=zanox&utm_medium=xml_Zanox&utm_campaign=Zanox&zpar9=4209ECD457683232DBD9" ["descriptionlong"]=> string(0) "" ["manufacturer"]=> string(2) "LG" ["ean"]=> int(7893299527609) ["image_url"]=> string(66) "https://static.wmobjects.com.br/imgres/arquivos/ids/2748054-250-250" ["merchantcategory"]=> string(12) "Eletr?nicos" ["merchantproductid"]=> string(6) "415012" ["merchantlogo"]=> string(110) "https://upload.wikimedia.org/wikipedia/commons/thumb/7/76/New_Walmart_Logo.svg/1280px-New_Walmart_Logo.svg.png" ["manufacturer_model"]=> string(0) "" }
It creashes always near the row 200, never before 150.
Sounds like you’re running out of memory. Try saving less data at a time, or using a more efficient process like WP CLI.
- The topic ‘Insert records into pods using php’ is closed to new replies.