Yii database Add, modify, delete related Operations summary
In this paper, we summarize the data in Yii, modify the data, delete the relevant operation of data, just learned a few days, only recorded some, later slowly enrich, there is a need for friends to see.
Ways to add data
(1) Save method (Object form operation)
$user =new User;
$user->username= ' phpernote ';
$user->password= ' 123456 ';
if ($user->save () >0) {
Echo ' Add success ';
}else{
Echo ' Add failed ';
}
(2) Insert method (array-type operation)
Yii::app ()->dbname->createcommand ()->insert (' Tbl_user ',
Array
' Username ' = ' phpernote ',
' Password ' = ' 123456 '
)
);
(3) Insert method (object form operation)
$user =new User ();
$user->username= ' phpernote ';
$user->password= ' 123456 ';
if ($user->insert ()) {
Echo ' Add success ';
}else{
Echo ' Add failed ';
}
Ways to modify data
(1) Post::model ()->updateall ($attributes, $condition, $params);
$count =user::model ()->updateall (Array (' username ' = ' phpernote ', ' password ' = ' 123456 '), ' Id=:id ', Array (': ID ' = $id));
if ($count >0) {
Echo ' modification succeeded ';
}else{
Echo ' modification failed ';
}
(2) Post::model ()->updatebypk ($PK, $attributes, $condition, $params);
$count =user::model ()->updatebypk (1,array (' username ' = ' admin ', ' password ' = ' 123456 '));
Or
$count =user::model ()->updatebypk (array), Array (' username ' = ' admin ', ' password ' = ' 123456 '), ' username =:name ', Array (': Name ' = ' admin '));
if ($count >0) {
Echo ' modification succeeded ';
}else{
Echo ' modification failed ';
}
$PK represents a primary key, which can be either a collection, $attributes represents a collection of fields to modify, $condition represents a condition, $params an incoming value
(3) Post::model ()->updatecounters ($counters, $condition, $params);
$count =user::model ()->updatecounters (Array (' status ' =>1), ' Username=:name ', Array (': Name ' = ' admin '));
if ($count >0) {
Echo ' modification succeeded ';
}else{
Echo ' modification failed ';
}
The array (' status ' =>1) represents the admin table in the database based on the condition username= ' admin ', and all the results that are queried automatically add 1 to the Status field.
User::model ()->updatecounters (Array (' Count ' =>1), ' id= '. User::model ()->id);//Auto Overlay 1
User::model ()->updatecounters (Array (' Count ' =>-1), ' id= '. User::model ()->id);//auto-decrement 1
(4) Yii::app ()->dbname->createcommand ()->update ($attributes, $condition, $params);
Yii::app ()->dbname->createcommand ()->update (' Tbl_user ',
Array
' Username ' = ' phpernote '
),
' Id=:id ',
Array
': Id ' =>3
)
);
Ways to delete data
(1) Post::model ()->deleteall ($condition, $params);
For example:
$count =user::model ()->deleteall (' Username=:name and password=:p ", Array (': Name ' = ' phpernote ', ':p ' > ' 123456 '));
Or:
$count =user::model ()->deleteall (' ID in (") '); Delete data with ID for these
if ($count >0) {
Echo ' Delete succeeded ';
}else{
Echo ' delete failed ';
}
(2) Post::model ()->deletebypk ($PK, $condition, $params);
For example:
$count =user::model ()->deletebypk (1);
Or:
$count =user::model ()->deletebypk (array), ' Username=:name ', Array (': Name ' = ' admin '));
if ($count >0) {
Echo ' Delete succeeded ';
}else{
Echo ' delete failed ';
}
Articles you may be interested in
- PHP to add a backslash in front of the reason and PHP to remove the backslash method, three ways to close the PHP magic quotes
- Mysql Database caching cache function analysis, debugging and performance summary
- PHP MySQL database operation class
- FCKeditor Add right-click menu-Picture Delete function
- Summarize the reason and solution of the MySQL database server gradually becoming slow
- MySQL server master-slave database synchronization configuration
- PHP Empty (delete) the file under the specified directory, do not delete the Directory folder method
- PHP uses Array_flip to implement array key-value Exchange to remove array duplicate values
http://www.bkjia.com/PHPjc/980975.html www.bkjia.com true http://www.bkjia.com/PHPjc/980975.html techarticle Yii Database Add, modify, delete related Operations Summary This article summarizes the data information in YII data, modify data, delete data related operations, just learned a few days, ...