Php Object Generator Tutorials
  1. Introduction to POG
  2. Setting up PHP, MySQL etc.
  3. Designing your objects
  4. Generating your code
  5. Description of the generated code
  6. Edit configuration file
  7. The Setup Process
  8. Using the code: Save()
  9. Using the code: Get()
  10. Using the code: SaveNew()
  11. Using the code: GetList()
  12. Using the code: Delete()
  13. Using the code: DeleteList()
  14. Advanced: object relations
  15. Advanced: Set{Parent}()
  16. Advanced: Get{Parent}()
  17. Advanced: Add{Child}()
  18. Advanced: Get{Child}List()
  19. Advanced: Save(deep)
  20. Advanced: Delete(deep)
  21. Advanced: Add{Sibling}()
  22. Advanced: Set{Child}List()
  23. Advanced: Set{Sibling}List()
  24. Advanced: Get{Sibling}List()
  25. Advanced: DeleteList(deep)
  26. Customizing POG-generated code
  27. Customizing: Extending POG Objects
  28. Customizing: Plugins
  29. Examples
  30. Examples: User registration system
  31. Examples: User authentication
  32. Examples: Survey form
  33. Examples: Using POG with AJAX
  34. PDO: Introduction
  35. PDO: SQLite example
  36. PDO: Firebird example
  37. PDO: PostgreSQL example
  38. PDO: MySQL example
  39. PDO: ODBC example
  40. Troubleshooting
  41. Troubleshooting: Data appears encoded
  42. Troubleshooting: Can't regenerate object
  43. Troubleshooting: Can't seem to Save()
  44. Troubleshooting: Can't get object / object attributes from database
  45. Troubleshooting: Can't open zip file on Mac
  46. Troubleshooting: Setup screen is blank
  47. Videos
  48. Appendix: Creating table(s) manually
  49. Appendix: Regenerating objects
  50. Appendix: Generating objects using SOAP
  51. Case Study: Gravity GTD
  52. Case Study: Web Form Factory

Want more Php Object Generator?
Back to the Code Generator
The POG Weblog and RSS feed.
The POG Google group

Using the code: Save()

You can watch a video which demonstrates the explanation below.

The Save() CRUD method allows you to insert an object into your database. If the object already exists in the database, the object will be updated instead.

Save() returns the object Id of the inserted/updated object.

For e.g this is what the generated code for the Save() function might look like:


To save an object, simply do the following:

$book = new Book()//create a book object
$book->title = "Php is cool"//assign a title to the book
$book->Save()//this will insert the following record into the table
$book->title = "Php is very cool"; //modify the title of the book
$book->Save()//this will update the record

You can also use the return value of the Save function for error handling:
$book = new Book()//create a book object
$book->title = "Php is cool"//assign a title to the book
$book->Save()//this will insert the following record into the table
$book->title = "Php is very cool"; //modify the title of the book
if ($book->Save())
    echo "book successfully saved";
    echo "something bad happened, couldn't save";

A common scenario is when you have to create a log in system to allow returning users to access their account.

$user= new User()//create a user object
$user->GetUserList("username", "=", $_POST["username"]); //get a list of user with username supplied
$currentUser = $userList[ 0 ]//assuming all usernames are unique, this should give you the correct user
if ($user->password == $_POST['password'])
    //log user in either by putting user id in the session or serializing the entire user
    $_SESSION['userId'] = $user->userId; // either put userid in session
    $_SESSION['user'] = serialize($user); //  or serialize user in session
    //user is not logged in
    echo "wrong username / password";

Another common scenario is when once a user is logged in, you want to allow him/her to modify some aspects of his/her profile on your website;

$user= new User()//create a user object
$user->Get($_SESSION['userId']); //Get the logged in user
//If you serialized the user in the example above, you can simply unserialize it
$user->firstName = $_POST['firstName'];
$user->lastName = $_POST['lastName'];
$user->Save()//this will update the user profile by updating the new firstname and lastname supplied

POG documentation summary: