• Resolved samurai79

    (@samurai79)


    Hi, I’m enjoying the plugin and what it can do to display data from Airtable. However, I’m wondering if you could provide example(s) of how to update a record with the plugin? Airtable’s API documentation provides the code for use with curl, but I’m guessing this isn’t PHP. I though that perhaps there might be some easier way of doing it through Airpress?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Chester McLaughlin

    (@chetmac)

    Great question—I spent a lot of time on the update/create aspects of the API and I’m excited to share my usage examples with you. I’ve created a video explaining the following code, you can check it out at https://youtu.be/68yfpgV-j0Q

    In case it’s not obvious to all reading this: The Airpress API is the underlying functionality *only* available via PHP coding. If you’re not comfortable editing your PHP theme files, then this isn’t for you. Thanks!

    <?php
    
    define("CONFIG_ID",0); // OR
    define("CONFIG_NAME","Default");
    
    // usage: AirpressQuery($tableName, CONFIG_ID or CONFIG_NAME)
    $query = new AirpressQuery("Cuisines", CONFIG_ID);
    $query->filterByFormula("{Name} = 'Burgers'");
    $query->setExpireAfter(0);
    
    $cuisines = new AirpressCollection($query);
    
    // usage: $AirpressCollection->populateRelatedField($columnName,$relatedTableName)
    $cuisines->populateRelatedField("Restaurants", 	"Restaurants");
    
    if ( is_airpress_empty($cuisines) ){
    	die("Nothing to update");
    }
    
    // We only expected/wanted a single cuisine record but
    // we wanted all the restaurants linked to that cuisine
    $burger_joints = $cuisines[0]["Restaurants"];
    
    if ( ! is_airpress_empty($burger_joints) ){
    
    	foreach($burger_joints as $restaurant){
    
    		// There may or may not be anything we want to update!
    		$fields_to_update = array();
    
    		if ($restaurant["Status"] != "Visited"){
    			$fields_to_update["Status"] = "Visited";
    			$fields_to_update["Reservation On"] = date("c",strtotime("yesterday"));
    		}
    
    		// This is so that we're not REsending ALL
    		// the records fields each time—if there's 
    		// no fields to update, then don't do it.
    		if ( ! empty($fields_to_update) ){
    			$restaurant->update($fields_to_update);
    		}
    
    	}
    }
    
    // Note that Cuisine is provided as a record ID wrapped in an array. ALL airtable related
    // records are arrays, even if it's a ONE-to-ONE relationship. That's just how they do it.
    $new_burger_joint_array = array("Name" => "Fat Joe's Famous Burger Joint","Cuisine" => array($cuisines[0]->record_id()));
    $new_burger_joint_record = $burger_joints->createRecord($new_burger_joint_array);
    
    echo $new_burger_joint_record->record_id(); // Will output the newly created record ID
    echo "<br>";
    echo $new_burger_joint_record["Name"]; // Will output Fat Joe's...
    echo "<br>";
    
    // OR if you don't want to have to use a collection:
    $new_burger_joint_data = AirpressConnect::create(CONFIG_ID,"Restaurants",$new_burger_joint_array);
    
    $new_query = new AirpressQuery("Restaurants",CONFIG_ID);
    $new_collection = new AirpressCollection($new_query,false);
    $new_collection->setRecords(array($new_burger_joint_data));
    
    $new_burger_joint_record = $new_collection[0];
    
    echo $new_burger_joint_record->record_id(); // Will output the newly created record ID
    echo "<br>";
    echo $new_burger_joint_record["Name"]; // Will output Fat Joe's...
    
    ?>
    Thread Starter samurai79

    (@samurai79)

    Awesome! Thanks for that, I’ll give it a whirl.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Update record example’ is closed to new replies.