1. Execute the PDO method of native SQL too.
Copy codeThe Code is as follows: $ SQL = ""; // original SQL statement
Xx: model ()-> dbConnection-> createCommand ($ SQL)-> execute ();
Ii. Active Record mode
(1) New Method
Copy codeThe Code is as follows: $ post = new Post;
$ Post-> title = 'sample post ';
$ Post-> content = 'Post body content ';
$ Post-> save ();
(2) Criteria Mode
You can also use $ condition to specify more complex query conditions. Without a string, we can make $ condition an instance of CDbCriteria, which allows us to specify conditions not limited to WHERE.
Copy codeThe Code is as follows: $ criteria = new CDbCriteria;
$ Criteria-> select = 'title'; // select only the 'title' Column
$ Criteria-> condition = 'postid =: postid ';
$ Criteria-> params = array (': postid' => 10 );
$ Post = Post: model ()-> find ($ criteria );
One alternative to CDbCriteria is to pass an array to the find method. The keys and values of the array correspond to the attribute names and values of the standard (criterion). The preceding example can be rewritten as follows:
Copy codeThe Code is as follows: $ post = Post: model ()-> find (array (
'Select' => 'title ',
'Condition' => 'postid =: postid ',
'Params' => array (': postid' => 10 ),
));
When a query condition is about matching several columns by specified value, we can use findByAttributes (). The $ attributes parameter is an array of values indexed by column names. In some frameworks, this task can be implemented by calling methods similar to findByNameAndTitle. Although this method looks attractive, it often causes confusion, conflicts, and Case sensitivity issues such as column names.
Iii. Query Builder
Copy codeThe Code is as follows: $ user = Yii: app ()-> db-> createCommand ()
-> Select ('Id, username, profile ')
-> From ('tbl _ user U ')
-> Join ('tbl _ profile P', 'U. id = p. user_id ')
-> Where ('Id =: id', array (': id' => $ id ))
-> QueryRow ();