-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
136 lines (107 loc) · 3.44 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace test;
/* Core */
use core\model\db\wDb;
use core\model\pdo\wPdo;
use core\model\orm\wOrm;
/* Models */
use model\tag\Tag;
use model\article\Article;
use model\commentaire\Commentaire;
/* Defines the autoload function */
spl_autoload_register(function($class) {
$tab = explode('\\', $class);
$path = implode(DIRECTORY_SEPARATOR, $tab) . '.class.php';
if(file_exists($path))
{
include_once($path);
return true;
}
throw new \Exception('Cannot load : ' . $class);
});
try {
/* Sets the connection */
$pdo = new wPdo('mysql:host=localhost;dbname=tests_orm', 'root', 'root');
$db = new wDb($pdo);
wOrm::setDataSource($db);
/* Tests */
echo '<b>Database overview :</b>';
echo '<br />';
echo '- ' . Article::count() . ' article(s)';
echo '<br />';
echo '- ' . Commentaire::count() . ' commentaire(s)';
echo '<br />';
echo '- ' . Tag::count() . ' tag(s)';
echo '<br />';
$article = Article::findOne(array('id' => 1));
echo '<br />';
echo 'L\'article intitulé "';
echo $article->getTitle();
echo '" comporte ';
echo $article->getNbCommentaires();
echo ' commentaire(s).';
echo '<br />';
echo '<br />';
echo '<b>Create a new \'Commentaire\' :</b>';
echo '<br />';
$comment = new Commentaire();
$comment->author = 'William';
$comment->message = 'Hi ! How are you ? That\'s pretty cool ! =P';
$comment->is_published = 1;
$comment->save();
echo 'It got the id : ' . $comment->getId() . '.';
echo '<br />';
echo '<br />';
echo '<b>Gonna retrieve it...</b>';
echo '<br />';
$a_comment = Commentaire::findOne(array('id' => $comment->getId()));
echo $a_comment->getMessage() . ' <em>by ' . $a_comment->getAuthor() . '</em>.';
echo '<br />';
echo '<br />';
echo '<b>Playing with relations :</b>';
echo '<br />';
echo 'Adds two comments to our article (1-N relation) : how many comments has it ?';
echo '<br />';
$comment2 = new Commentaire();
$comment2->author = 'William (again)';
$comment2->message = 'Hi ! This comment will not be registered until the article be saved.';
$comment2->is_published = 1;
echo 'Before: ' . $article->getNbCommentaires();
echo '<br />';
$article->setCommentaires(array($comment, $comment2));
echo 'After: ' . $article->getNbCommentaires();
echo '<br />';
echo '<br />';
echo 'Comment\'s article :';
echo '<br />';
echo '<ul>';
foreach($article->getCommentaires() as $c) {
echo '<li>' . $c->getAuthor() . ' <i>said</i> ' . $c->getMessage() . '</li>';
}
echo '</ul>';
echo '<br />';
echo '<b>And now ?</b>';
echo '<br />';
echo '- ' . Article::count() . ' article(s)';
echo '<br />';
echo '- ' . Commentaire::count() . ' commentaire(s)';
echo '<br />';
echo '- ' . Tag::count() . ' tag(s)';
echo '<br />';
echo '<br />';
echo '<b>Delete the comment !</b>';
$comment->delete();
echo '<br />';
echo '<br />';
echo '<b>The end !</b>';
echo '<br />';
echo '- ' . Article::count() . ' article(s)';
echo '<br />';
echo '- ' . Commentaire::count() . ' commentaire(s)';
echo '<br />';
echo '- ' . Tag::count() . ' tag(s)';
echo '<br />';
echo '<br />';
} catch(\Exception $e) {
echo 'Ooops! ' . $e->getMessage() . '<br />';
}