Yii Framework connecting to database study notes

Source: Internet
Author: User
Tags php file first row yii

Configure the database connection in the protected/config/main. Php file:

The code is as follows: Copy code


'DB' => array (
'Connectionstring' => 'MySQL: host = localhost; dbname = testdrive ',
'Default' => true,
'Username' => 'root ',
'Password' => '',
'Charset' => 'utf8 ',
),

Define database operation class: models

The code is as follows: Copy code


<? Php
 
Class User extends CActiveRecord {
Public static function model (_ CLASS __){
 
Return parent: model ($ className );
 
} Www.111cn.net
// The static method model () is required by every AR class.
 
Public function tableName (){
 
Return '{User }}';
 
}
 }
 
?>

Instance database operation class: controllers

The code is as follows: Copy code


<? Php
Class UserController extends Controller {
 
Public function actionIndex (){
 
$ Model = new User;
$ Model-> username = 'admin ';
$ Model-> password = '000000 ';
$ Model-> email = 'admin @ admin.com ';
Var_dump ($ model-> save ());
Exit;
$ This-> render ('index ');
 
}
 
}
 
?>

Read records:

 

The code is as follows: Copy code

// Search for a row in the result that meets the specified condition
 
$ User = User: model-> find ($ condition, $ params );
 
// Find the row with the specified primary key.
 
$ User = User: model-> findByPk ($ postId, $ condition, $ params );
 
// Search for rows with the specified property value
 
$ User = User: model-> findByAttributes ($ attributes, $ condition, $ params );
 
// Use the specified SQL statement to find the first row in the result
$ User = User: model-> findBySql ($ SQL, $ params );
 
// Search for all rows that meet the specified conditions
 
$ Posts = Post: model ()-> findAll ($ condition, $ params );
 
// Search for all rows with the specified primary key
 
$ Posts = Post: model ()-> findAllByPk ($ postIDs, $ condition, $ params );
 
// Search for all rows with the specified property value
 
$ Posts = Post: model ()-> findAllByAttributes ($ attributes, $ condition, $ params );
 
// Query all rows using the specified SQL statement
 
$ Posts = Post: model ()-> findAllBySql ($ SQL, $ params );
We can make $ condition an instance of CDbCriteria.


$ 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); // $ params is not required
// Obtain the number of rows that meet the specified condition www.111cn.net
 
$ N = Post: model ()-> count ($ condition, $ params );
 
// Obtain the number of result rows through the specified SQL statement
 
$ N = Post: model ()-> countBySql ($ SQL, $ params );
 
// Check whether at least one row of compound conditions is specified
 
$ Exists = Post: model ()-> exists ($ condition, $ params );

Update record:

The code is as follows: Copy code

 

$ Post = Post: model-> findByPk (10 );
 
$ Post-> title = 'new post title ';
 
$ Post-> save (); // save the changes to the database
 
// Update the row that meets the specified condition
 
Post: model-> updateAll ($ attributes, $ condition, $ params );
 
// Update the row that meets the specified condition and primary key
 
Post: model-> updateByPk ($ pk, $ attributes, $ condition, $ params );
 
// Update the count column of the row that meets the specified condition
 
Post: model-> updateCounters ($ counters, $ condition, $ params );


Delete data:

The code is as follows: Copy code

 

$ Post = Post: model ()-> findByPk (10); // assume that there is a post whose ID is 10.
 
$ Post-> delete (); // delete this row from the data table
 
// Delete the row that meets the specified condition
 
Post: model ()-> deleteAll ($ condition, $ params );
 
// Delete a row that meets the specified condition and primary key
 
Post: model ()-> deleteByPk ($ pk, $ condition, $ params );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.