rpersistence Wiki
Brought to you by:
rnc1ar
<?php
// The first step is include the framework main class.
// That's all.. Now you can use the framework.
require('rpersistence/rpersistence.php');
// Get the framework session
$session = RPersistence::getSession();
// For example we will insert a new user in the database.
try {
// Create a new user
$user = new User();
$user->setId(0);
$user->setUser("rodrigo.4ar");
$user->setPassword("123456");
// Methods save, update, delete
// automatically connects and disconnects from the database
// Create a new user
$session->createTransaction()->save($user);
} catch(Exception $e) {
$e->showErrorPage();
}
?>
<?php
try {
// Get all records for users (entity)
$users = $session->getTableFromCache("User");
} catch(Exception $e) {
$e->showErrorPage();
}
?>
<?php
try {
// Retrives objects from cache (entity, query)
$users = $session->getObjectFromCacheByQuery("User", "select id, username, passwd from users");
} catch(Exception $e) {
$e->showErrorPage();
}
?>
<?php
try {
// If the table USERS is not in cache this method will load the table,
// and retrives an object by PK (entity, pk value)
$users = $session->getObjectFromCacheByPk("User", 1);
} catch(Exception $e) {
$e->showErrorPage();
}
?>
In some cases you will need query the database without caching.
<?php
try {
// Retrives objects from database (without caching) (entity, query)
$users = $session->getObjectByQuery("User", "select id, username, passwd from users");
} catch(Exception $e) {
$e->showErrorPage();
}
?>
Sometimes we need the connection object to do a custom action.
<?php
// Get connection object
$db = $session->getConnection();
// Open the connection
$db->openConnection();
// Do stuff
// Get an array
$array = $db->fetchArray("select * from users");
// Get a resultset
$rs = $db->executeReader("select * from users");
// Exucte a DDL Query
$db->executeNonQuery("INSERT, UPDATE or DELETE query");
// Don't forget close the connection!!
$db->closeConnection();
?>