-
Notifications
You must be signed in to change notification settings - Fork 19
/
README
140 lines (89 loc) · 3.66 KB
/
README
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
137
138
139
140
## Installation ##
http://jien.jaequery.com/docs/introduction
** Live Demo **
Check out our live demo at:
http://jien.jaequery.com/admin
user: admin
pass: demo
** Jien Model **
*** The query methods (where, limit, orderBy, groupBy, having) are all chainable so you can mix/match them.
// get a single record by it's primary id
$data = Jien::model("User")->get(16)->row();
// get a single record with a where clause
$data = Jien::model("User")->where("u.username LIKE '%jae%')->get()->row();
// get all records
$data = Jien::model("User")->get()->rows();
// custom selects
$data = Jien::model("User")->select("count(*) as count, u.username, u.user_id)->get()->rows();
// get all records and limit to 20
$data = Jien::model("User")->limit(20)->get()->rows();
// get all records and limit to 20 and offset by 10
$data = Jien::model("User")->limit(20, 10)->get()->rows();
// sort by username desc
$data = Jien::model("User")->limit(20)->orderBy('u.username desc')->get()->rows();
// where clause for male only
$data = Jien::model("User")->where("u.gender = 'm'")->orderBy('u.user_id asc')->get()->rows();
// where clause for male or female
$data = Jien::model("User")->where("u.gender = 'm' OR u.gender='f'")->get()->rows();
// alternatively ...
$data = Jien::model("User")->where("u.gender = 'm'")->orWhere("u.gender = 'f'")->orderBy('u.username asc')->get()->rows();
// get all records and where clause for male only and add a or where clause
$data = Jien::model("User")->where("u.gender = 'm'")->orWhere("u.gender = 'f'")->get()->rows();
// get all records and where clause for male only and add a or where clause
$data = Jien::model("User")->where("u.gender = 'm'")->orWhere("u.gender = 'f'")->get()->rows();
// group by gender
$data = Jien::model("User")->groupBy("u.gender")->get()->rows();
// group by with having
$posts = Jien::model("User")
->leftJoin("Post p", "p.user_id = u.user_id")
->where("p.user_id IS NOT NULL")
->groupBy("u.gender")
->having("u.active=1")
->get()->rows();
// left join takes 2 params, 3rd optional (table and alias(optional), join condition, fields to select)
$data = Jien::model("Post")
->select("p.subject, p.message")
->leftJoin("User u", "u.user_id = p.user_id", "u.username, u.user_id, p.created as post_created")
->get()->rows();
Jien::debug($data);
*** From the User model ***
You can create pre-defined chunks of sql to re-use / interchange easily:
public function byHuman(){
$this->andWhere("u.gender = 'f' OR u.gender = 'm'");
return $this;
}
...
Now you can just:
$data = Jien::model("User")->byHuman()->byPopular()->etc..etc..->get()->rows();
...
And pagination is simple too:
[controller]
$current_page = $this->_getParam('page', 1);
$this->view->data = Jien::model("User")->withPager($current_page)->get();
or to override defaults (defaults are item_per_page = 10, page_range = 10)
$item_per_page = 10;
$page_range = 10;
$this->view->data = Jien::model("User")->withPager($current_page, $item_per_page, $page_range)->get();
[index.phtml]
<?php echo $this->pager($this->data->pager(), 'Sliding', 'partials/pager/pager.phtml'); ?>
<?php Jien::debug($this->data->rows()); ?>
*** Save ***
$id = Jien::model("User")->save(array(
"username" => "someguy123",
));
*** Update ***
just pass it's primary value to the data:
$affected = Jien::model("User")->save(array(
"user_id" => 23,
"username" => "someguy123",
));
or pass a wheres clause...
$affected = Jien::model("User")->save(array(
"username" => 'newname',
), "gender = 'm'");
*** Delete ***
$affected = Jien::model("User")->delete($id);
or
$affected = Jien::model("User")->delete("gender = '' AND a != '2'");
----------
I let my request object be responsible for filtering out sql injection ...