- Introduction to POG
- Setting up PHP, MySQL etc.
- Designing your objects
- Generating your code
- Description of the generated code
- Edit configuration file
- The Setup Process
- Using the code: Save()
- Using the code: Get()
- Using the code: SaveNew()
- Using the code: GetList()
- Using the code: Delete()
- Using the code: DeleteList()
- Advanced: object relations
- Advanced: Set{Parent}()
- Advanced: Get{Parent}()
- Advanced: Add{Child}()
- Advanced: Get{Child}List()
- Advanced: Save(deep)
- Advanced: Delete(deep)
- Advanced: Add{Sibling}()
- Advanced: Set{Child}List()
- Advanced: Set{Sibling}List()
- Advanced: Get{Sibling}List()
- Advanced: DeleteList(deep)
- Customizing POG-generated code
- Customizing: Extending POG Objects
- Customizing: Plugins
- Examples
- Examples: User registration system
- Examples: User authentication
- Examples: Survey form
- Examples: Using POG with AJAX
- PDO: Introduction
- PDO: SQLite example
- PDO: Firebird example
- PDO: PostgreSQL example
- PDO: MySQL example
- PDO: ODBC example
- Troubleshooting
- Troubleshooting: Data appears encoded
- Troubleshooting: Can't regenerate object
- Troubleshooting: Can't seem to Save()
- Troubleshooting: Can't get object / object attributes from database
- Troubleshooting: Can't open zip file on Mac
- Troubleshooting: Setup screen is blank
- Videos
- Appendix: Creating table(s) manually
- Appendix: Regenerating objects
- Appendix: Generating objects using SOAP
- Case Study: Gravity GTD
- Case Study: Web Form Factory
Back to the Code Generator
The POG Weblog and RSS feed.
The POG Google group
Using the code: Save()
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:
EXAMPLES:
To save an object, simply do the following:
PHP:
$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
$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:
PHP:
$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";
}
else
{
echo "something bad happened, couldn't save";
}
$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";
}
else
{
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.
PHP:
$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
}
else
{
//user is not logged in
echo "wrong username / password";
}
$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
}
else
{
//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;
PHP:
$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->unserialize($_SESSION['user']);
$user->firstName = $_POST['firstName'];
$user->lastName = $_POST['lastName'];
$user->Save(); //this will update the user profile by updating the new firstname and lastname supplied
$user->Get($_SESSION['userId']); //Get the logged in user
//If you serialized the user in the example above, you can simply unserialize it
$user->unserialize($_SESSION['user']);
$user->firstName = $_POST['firstName'];
$user->lastName = $_POST['lastName'];
$user->Save(); //this will update the user profile by updating the new firstname and lastname supplied



You can watch a video which demonstrates the explanation below.