-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtest.php
76 lines (71 loc) · 2.18 KB
/
test.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
<?php
include "ActiveRecord.php";
//include "ActiveRecord.min.php";
class User extends ActiveRecord{
public $table = 'user';
public $primaryKey = 'id';
public $relations = array(
'contacts' => array(self::HAS_MANY, 'Contact', 'user_id'),
'contact' => array(self::HAS_ONE, 'Contact', 'user_id', array('where' => '1', 'order' => 'id desc'), 'user'),
);
}
class Contact extends ActiveRecord{
public $table = 'contact';
public $primaryKey = 'id';
public $relations = array(
'user' => array(self::BELONGS_TO, 'User', 'user_id'),
);
}
ActiveRecord::setDb(new PDO('sqlite:test.db', null, null, array(PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION)));
try {
ActiveRecord::execute("CREATE TABLE IF NOT EXISTS user (");
ActiveRecord::execute("select * from aaa");
} catch( Exception $e) {
var_export($e);
}
ActiveRecord::execute("CREATE TABLE IF NOT EXISTS user (
id INTEGER PRIMARY KEY,
name TEXT,
password TEXT
);");
ActiveRecord::execute("CREATE TABLE IF NOT EXISTS contact (
id INTEGER PRIMARY KEY,
user_id INTEGER,
email TEXT,
address TEXT
);");
$user = new User();
$user->name = 'demo';
$user->password = md5('demo');
var_dump($user->insert());
$contact = new Contact();
$contact->address = 'test';
$contact->email = '[email protected]';
$contact->user_id = $user->id;
var_dump($contact->insert());
$contact = new Contact();
$contact->address = 'test';
$contact->email = '[email protected]';
$contact->user_id = $user->id;
var_dump($contact->insert());
var_dump($user->contact);
echo "\n -----";
var_dump($user);
echo "\n join\n";
$contact = new Contact();
var_dump($contact->select('user.*, contact.*')->join('user', 'user.id = contact.user_id')->find());
/*
$contact = new Contact();
$contact->address = 'test';
$contact->email = '[email protected]';
$contact->user_id = 2;
var_dump($contact->insert());
*/
$user = new User();
var_dump($user->select('user.*, c.email, c.address')->join('contact as c', 'c.user_id = user.id')->findAll());
var_dump($user->reset()->notnull('id')->orderby('id desc')->find());
echo "\nContact of User # {$user->id}\n";
var_dump($user->contacts);
$contact = new Contact();
var_dump($contact->find());
var_dump($contact->user);