1. simple query $ userM (& amp; #39; user & amp; #39;); // query a piece of information and return a one-dimensional array $ list $ user-& gt; find (); // query all information, returns the two-dimensional array select or findAll ()...
1. simple query
- $ User = M ('user ');
- // Query a piece of information and return a one-dimensional array
- $ List = $ user-> find ();
- // Query all information, returns the two-dimensional array select or findAll ()
- $ List = $ user-> select ();
2. coherent operations in queries
(1) where method
- $ User-> where ('status = 1')-> order ('create _ time')-> limit (10)-> select ();
- $ User-> select (array ('order' => 'Create _ time', 'where' => 'status = 1 ', 'limit' => '10 '));
- $ User-> where ('Id = 1')-> field ('Id, name, email ')-> find ();
- $ User-> where ('status = 1 and id = 1')-> delete ();
- $ List = $ user-> where ('Id> 1')-> limit ('5')-> order ('Id desc')-> field ('Id, username')-> select ();
(2) cross-table query $ user-> table ()
- $ List = $ user-> table ('aoli _ user u, aoli_test t')-> where ('U. id = t. id')-> select ();
(3) the $ user-> data () data method can be replaced. it is mainly used for the save, add, and delete operations to save data.
- $ Model-> data ($ data)-> add ();
- $ Model-> data ($ data)-> where ('Id = 3')-> save ();
Parameters of the Data method support objects and arrays. If an object is an array, it is automatically converted. if the data method is not defined, you can use the create method or manually assign values to the Data object.
(4), $ user-> field ()
Specifies which fields to query. the parameter can be a string field ('user. id as uid, m. id as mid '), or an array field (array ('user. ID' => 'uid', 'M. ID' => 'mid '))
(5), $ user-> order ()
The parameter can also be a string order ('Id desc') or an array order (array ('id' => 'desc', 'Username '))
- $ Model-> field ('Id, nickname as name')-> select ();
- $ Model-> field (array ('id', 'nickname' => 'name')-> select ();
(6), limit ()
There are two parameters. The first parameter indicates the starting position and the second parameter specifies the number of queries.
Limit ('8'): Query 8 records starting from 1st.
Limit ('3, 8') starts from 3rd and queries 8
(7) page () query pages
(8) group ()
$ User-> group ('')-> select ()
(9) The having method is mainly used for secondary filtering.
(10) join queries. common join queries include INNER join, left join, and right join. the default value is left join.
(11) unique filtering by distinct
Select distinct id, username from ..
Filter out duplicate filtering
$ User-> distinct (true)-> select ();
(12) relation method association model
(13), lock query lock (true)
3. update data save ()
- $ User = M ('user ');
- $ Data ['password'] = 'aaa ';
- $ List = $ user-> where ('Id = 1')-> save ($ data );
- // Or
- $ User = M ('user ');
- $ Data ['password'] = 'aaa ';
- $ Data ['id'] = '4 ';
- $ List = $ user-> save ($ data );
- // Or
- $ User = M ('user ');
- $ Data ['password'] = 'aaa ';
- $ Data ['id'] = '4 ';
- $ List = $ user-> data ($ data)-> save ();
Create to update data
Create.html:
-
-
-
-
-
-
-
UserAction. class. php
- Function create (){
- $ This-> display ();
- }
- Function addit (){
- $ User = M ('user ');
- If ($ vo = $ user-> create ()){
- // $ User-> password = md5 ($ user-> password );
- If ($ user-> save ()){
- $ This-> success ('update successful ');
- } Else {
- $ This-> error ('update failed ');
- }
- }
- }
$ Vo indicates the number of affected rows, that is, the number of rows Updated. other methods such as setInc (usually numeric fields for operations), the first write segment, and the second write condition, the third write needs to add an integer:
$ List = $ user-> setInc ('price', 'id = 1', 3); // Add 3 to the price Field
SetDec is the subtraction operation.
$ List = $ user-> setDec ('price', 'id = 1', 3); // The price field minus 3
SetField single record targeted modification
$ User-> where ('Id = 1')-> setField (array ('username', 'password'), array ('Google ', 'baidu '));
4. add () data ()
- $ User = M ('user ');
- $ Data ['password'] = 'aaa ';
- $ List = $ user-> data ($ data)-> add ();
$ List is the primary key value after insertion, or you can use createg to add:
- Function addit (){
- $ User = M ('user ');
- $ Vo = $ user-> create ();
- $ User-> add ();
- }
The value after form post is compressed to $ vo as an array by using the create method.
5. delete data ()
- Function del (){
- $ User = M ('user ');
- $ List = $ user-> delete (25 );
- Dump ($ list );
- }
- // Or
- Function del (){
- $ User = M ('user ');
- $ List = $ user-> where ('Id> 5')-> delete ();
- Dump ($ list );
- }