26
Apr/09
7

Quercus and App Engine – Reading from the data store.

Using brian’s scripts a base and google’s jdo documentation, I’ve got a working example of reading from the app engine data store with php.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
import phptest.test;
import phptest.PMF;
 
$p = PMF::get();
$pm = $p->getPersistenceManager();
$q = $pm->newQuery("select from phptest.test");
$results = $q->execute();
//Returns a php array of resources. Foreach doesn't seem to work here. This seems to be an issue with Quercus.
$account = count($results);
for($a=0;$a < $account; $a++){
	echo $results[$a]->getId() . " - - " . $results[$a]->getFirstName() . '<br/>';
}
$pm->close();
?>

Its just that easy.

Deletes work the same way too. The JDO persistence manager has a method called ‘deletePersistentAll()’ which will truncate the table. To delete a specific item, select it and call ‘deletePersistent(obj)’ where the obj is your record object. See the comments below.

JDO updates your data automatically. Select the item. Change the data and when you call the persistence managers close method, the changes will be saved.

Need more info: Read the google docs.

Comments (7) Trackbacks (0)
  1. Kali West
    11:09 pm on May 15th, 2009

    I want to say, that I really love your blog.
    Please go on like that and don’t stop posting. I hope this comment motivates you to do so, smile
    regards, kali

  2. Herbert Groot Jebbink
    11:48 pm on May 16th, 2009

    Hi, I’m have difficulties to implement the UD part of CRUD for this example. Below attemp to delete gives a 500 with java.lang.UnsupportedOperationException


    import phptest.test;
    import phptest.PMF;

    $p = PMF::get();
    $pm = $p->getPersistenceManager();
    $q = $pm->newQuery("select from phptest.test where id == 14");
    $results = $q->execute();

    echo $results[0]->getId() . " - " . $results[0]->getFirstName() . '';

    $pm->deletePersistent($results[0]);

    $pm->close();

  3. derak
    9:08 pm on May 20th, 2009

    I see what you mean.

    As a workaround, you could call:

    $q->deletePersistentAll();

    The query only has one record loaded and that is the only record that will be deleted.
    This worked for me.

    Google has a 1000 record limit for queries so you’ll need to build that logic into your grid if your expecting more rows than that. See this link for more info on quotas and limits..

    For update, call the set method you defined in the java table class.
    $results[0]->setId(YOUR_VALUE_HERE);

  4. Herbert Groot Jebbink
    12:38 pm on May 23rd, 2009

    Hi, I moved to the Quercus from Resin 4.0 and that solved a lot of problems, including the problem here described. However, your method is off course faster when deleting more records. (IMHO a interactive web-apps should never hit that 1000 record limit)

    I’m now working on a SQL to JDO wrapper. That way you can use ‘normal’ SQL statements in your PHP apps.

  5. Herbert Groot Jebbink
    4:36 am on May 24th, 2009

    Hi, see the URL below for an proof of concept how to use SQL CRUD (Create, Retrieve, Update, Delete) in PHP at Google App Engine.

    http://sql-to-jdo-for-php-at-gae.appspot.com

  6. JaneRadriges
    5:34 pm on June 13th, 2009

    The article is ver good. Write please more

  7. KattyBlackyard
    9:35 pm on June 14th, 2009

    Hi, interest post. I’ll write you later about few questions!

Leave a comment

No trackbacks yet.