PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Insert, Update, and Delete Customer Items

A customer/owner can access his own Customer Items feed to insert, update, or delete their items. These operations do not apply to the public snippets feed.

You can test a feed operation before it is actually executed by setting the dry-run flag ($dryRun) to TRUE. Once you are sure that you want to submit the data, set it to FALSE to execute the operation.

Insert an Item

Items can be added by using the insertGbaseItem() method for the Base service:

<?php
$service 
= new Zend_Gdata_Gbase($client);
$newEntry $service->newItemEntry();

// Add title
$title "PHP Developer Handbook";
$newEntry->title $service->newTitle(trim($title));

// Add some content
$content "Essential handbook for PHP developers.";
$newEntry->content $service->newContent($content);
$newEntry->content->type 'text';

// Define product type
$itemType "Products";
$newEntry->itemType $itemType;

// Add item specific attributes
$newEntry->addGbaseAttribute("product_type""book""text");
$newEntry->addGbaseAttribute("price""12.99 USD""floatUnit");
$newEntry->addGbaseAttribute("quantity""10""int");
$newEntry->addGbaseAttribute("weight""2.2 lbs""numberUnit");
$newEntry->addGbaseAttribute("condition""New""text");
$newEntry->addGbaseAttribute("author""John Doe""text");
$newEntry->addGbaseAttribute("edition""First Edition""text");
$newEntry->addGbaseAttribute("pages""253""number");
$newEntry->addGbaseAttribute("publisher""My Press""text");
$newEntry->addGbaseAttribute("year""2007""number");
$newEntry->addGbaseAttribute("payment_accepted""Google Checkout""text");

$dryRun true;
$createdEntry $service->insertGbaseItem($newEntry$dryRun);

Modify an Item

You can update each attribute element of an item as you iterate through them:

<?php
// Update the title
$newTitle "PHP Developer Handbook Second Edition";
$entry->title $service->newTitle($newTitle);

// Find <g:price> attribute and update the price
$baseAttributes $entry->getGbaseAttribute("price");
if (
is_object($baseAttributes[0])) {
  
$newPrice "16.99 USD";
  
$baseAttributes[0]->text $newPrice;
}

// Find <g:pages> attribute and update the number of pages
$baseAttributes $entry->getGbaseAttribute("pages");
if (
is_object($baseAttributes[0])) {
  
$newPages "278";
  
$baseAttributes[0]->text $newPages;

  
// Update the attribute type from "number" to "int"
  
if ($baseAttributes[0]->type == "number") {
    
$newType "int";
    
$baseAttributes[0]->type $newType;
  }
}

// Remove <g:label> attributes
$baseAttributes $entry->getGbaseAttribute("label");
foreach (
$baseAttributes as $note) {
  
$entry->removeGbaseAttribute($note);
}

// Add new attributes
$entry->addGbaseAttribute("note""PHP 5""text");
$entry->addGbaseAttribute("note""Web Programming""text");

// Save the changes by invoking save() on the entry object itself
$dryRun true;
$entry->save($dryRun);

// Or, save the changes by calling updateGbaseItem() on the service object
// $dryRun = true;
// $service->updateGbaseItem($entry, $dryRun);

After making the changes, either invoke save($dryRun) method on the Zend_Gdata_Gbase_ItemEntry object or call updateGbaseItem($entry, $dryRun) method on the Zend_Gdata_Gbase object to save the changes.

Delete an Item

You can remove an item by calling deleteGbaseItem() method:

<?php
$dryRun 
false;
$service->deleteGbaseItem($entry$dryRun);

Alternatively, you can invoke delete() on the Zend_Gdata_Gbase_ItemEntry object:

<?php
$dryRun 
false;
$entry->delete($dryRun);

Zend Framework