Exemples
Pour vous connecter à votre base de données avec PDO, utilisez le code suivant :
// Login to database
$database = 'tests';
$host = 'localhost';
$username = 'tests';
$password = 'tests';
$pdo = new PDO('mysql:dbname='.$database.';host='.$host.';',$username,$password);
Chien
Diagramme des classes
Définitions
(s)[dog:|name,string|]
Utilisation du code généré
// Creation of dogs
Dog::create($pdo,'Bill');
Dog::create($pdo,'Droopy');
Dog::create($pdo,'Milou');
// Display the names of dogs
$statement = Dog::selectAll($pdo);
while($dog = Dog::fetch($pdo,$statement)) {
echo $dog->getName();
}
Personnes et voitures
Diagramme des classes
Définitions
(s)[person:|firstname,string|,|lastname,string|,-car]
(s)[car:|model,string|,|brand,string|,*person]
Utilisation du code généré
// Creation of a person
$bond = Person::create($pdo,'James','Bond');
// Creation of cars
$vanquish = Car::create($pdo,'Vanquish','Aston Martin');
$esprit = Car::create($pdo,'Esprit','Lotus');
$fastback = Car::create($pdo,'Fastback','Mustang');
// Set the person as owner of the cars
$bond->addCar($vanquish);
$bond->addCar($esprit);
$bond->addCar($fastback);
// List person's cars
foreach ($bond as $car) {
echo $car;
}
Membres, messages et posts
Diagramme des classes
Définitions
(s)[member: |login,string/20|, |isAdmin,bool,,false|, |password,string/41|, *{sent,1}message, *{received,2}message]
(s)[message: {sender,1}member, {recipient,2}member, |title,string/20|, |date,datetime|, |content,string|]
[post: member, |title,string/20|, |date,datetime|, |content,string|]
(s)[post<-question: *answer]
(s)[post<-answer: question]
Utilisation du code généré
// Creation of members
$vador = Member::create($pdo,'Dark Vador',sha1('shmi'),true);
$luke = Member::create($pdo,'Luke Skywalker',sha1('force'));
// List all members
foreach (Member::loadAll($pdo) as $member) {
echo $member;
}
// Creation of a message between the members
$message = Message::create($pdo,$vador,$luke,'Important',time(),'I\'m your father');
// Creation of a question and an answer
$question = Question::create($pdo,$luke,'Why ?',time(),'...');
$answer = Answer::create($pdo,$vador,'Because',time(),'...',$question);
// List question's answers
$answers = $question->selectAnswers();
while ($answer = Answer::fetch($pdo,$answers)) {
echo $answer;
}