Exemplo n.º 1
0
 */
class UserMapper extends Horde_Rdo_Mapper
{
}
$um = new UserMapper($conf['adapter']);
// Count all users.
$userCount = $um->count();
echo "# users: {$userCount}\n";
// Get the number of new users in May 2005
//$userCount = $um->count('created > \'2005-05-01\' AND created <= \'2005-05-31\'');
//echo "# new: $userCount\n";
// Check if id 1 exists.
$exists = $um->exists(1);
echo "exists: " . ($exists ? 'yes' : 'no') . "\n";
// Look for Alice
$userTwo = $um->findOne(array('name' => 'Alice'));
if ($userTwo) {
    echo "Found Alice: id {$userTwo->id}\n";
} else {
    echo "No Alice found, creating:\n";
    // $userOne = $um->create(array('name' => 'Alice', 'phone' => '212-555-6565'));
    $userOne = new User(array('name' => 'Alice', 'phone' => '212-555-6565'));
    $userOne->setMapper($um);
    $userOne->save();
    $userOneId = $userOne->id;
    echo "Created new user with id: {$userOneId}\n";
}
// Change the name of the user and save.
if ($userTwo) {
    $userTwo->name = 'Bob';
    $result = $userTwo->save();
Exemplo n.º 2
0
<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');
require_once "Autoloader.php";
$db = new PdoAdapter();
$userMapper = new UserMapper($db);
$articleMapper = new ArticleMapper($db);
//get user by username
$user = $userMapper->findOne(array("conditions" => array('username = ? ', "john doe")));
echo "Username: "******"<br />Articles of this user:<br />";
foreach ($user->articles as $article) {
    echo $article->title . '<br />';
}
//edit the user
$user->username = "******";
$userMapper->update($user);
//add a new user
$newUser = new User();
$newUser->username = "******";
$userMapper->insert($newUser);
//delete a user
$userMapper->delete(4);
//get users with their articles
//get all users limited by 2, sorted by username ascending and eager load the article relationship
//only 2 queries - one for fetching the users and one for coupling the related articles
$users = $userMapper->findMany(array("limit" => 3, "sort" => array("username", "asc"), "relations" => array("Article")));
//4 queries: one for retrieving the users and 1 per user lazy loading the articles.
$users = $userMapper->findMany(array("limit" => 3, "sort" => array("username", "asc")));
echo sizeof($users) . " users found<br />";