


About the addition, deletion and modification of Yii framework
This article mainly introduces the addition, deletion, modification and checking of Yii framework. It has certain reference value. Now I share it with everyone. Friends in need can refer to it
1. Query data
1. findAll (query a collection based on a condition)
$admin=Admin::model()->findAll($condition,$params); $admin=Admin::model()->findAll("username=:name",array(":name"=>$username)); $admin=Admin::model()->findAll(“username=:name and age=:age” , array(“:name”=>$name, “age”=>$age)); $admin=Admin::model()->findAll(“username like :name and age=:age” , array(“:name”=>$name, “age”=>$age)); $infoArr= NewsList::model()->findAll("status = '1' ORDER BY id DESC limit 10 ");
2. findAllByPk (query a collection based on the primary key, multiple primary keys can be used)
$admin=Admin::model()->findAllByPk($postIDs,$condition,$params); $admin=Admin::model()->findAllByPk($id,"name like :name and age=:age",array(':name'=>$name,'age'=>$age)); $admin=Admin::model()->findAllByPk(array(1,2));
3. findAllByAttributes (query a collection based on conditions, which can be multiple conditions, and put the conditions into the array)
$admin=Admin::model()->findAllByAttributes($attributes,$condition,$params); $admin=Admin::model()->findAllByAttributes(array('username'=>'admin'));
4. findAllBySql( Query an array based on the SQL statement)
$admin=Admin::model()->findAllBySql($sql,$params); $admin=Admin::model()->findAllBySql("select * from admin where username like :name",array(':name'=>'%ad%'));
5. findByPk(Query an object based on the primary key)
$admin=Admin::model()->findByPk($postID,$condition,$params); $admin=Admin::model()->findByPk(1);
6. find (query a set of data based on a condition, there may be multiple data, only the first row of data is returned)
$row=Admin::model()->find($condition,$params); $row=Admin::model()->find('username=:name',array(':name'=>'admin'));
7. findByAttributes (query a set of data based on conditions, which can be multiple conditions, put the conditions into the array, and query the first piece of data)
$admin=Admin::model()->findByAttributes($attributes,$condition,$params); $admin=Admin::model()->findByAttributes(array('username'=>'admin'));
8 , findBySql (query a set of data based on SQL statements, query the first data)
$admin=Admin::model()->findBySql($sql,$params); $admin=Admin::model()->findBySql("select * from admin where username=:name",array(':name'=>'admin'));
9, count (query how many records there are in a collection based on a condition , returns an int type number)
$count=Post::model()->count($condition,$params); $count=Post::model()->count("username=:name",array(":name"=>$username));
10. countBySql (queries how many records a collection has according to the SQL statement and returns an int type number)
$count=Post::model()->countBySql($sql,$params); $count=Post::model()->countBySql("select * from admin where username=:name",array(':name'=>'admin'));
11. exists (query according to a condition to see if the array obtained has data. If there is data, return a true, otherwise it is not found)
$exists=Post::model()->exists($condition,$params); $exists=Post::model()->exists("name=:name",array(":name"=>$username));
2. Add data
save(add data)
$admin=new Admin; $admin->username =$username; $admin->password =$password; if($admin->save() > 0){echo"添加成功"; }else{echo"添加失败"; }
3. Modify data
update($pk primary key, which can be one or a set, $attributes is the set of fields to be modified, $condition, the value passed in by $params)
Post::model()->updateAll($attributes,$condition,$params); $count=Admin::model()->updateAll(array('username'=>'11111','password'=>'11111'),'password=:pass',array(':pass'=>'1111a1')); if($count> 0){echo "修改成功"; }else{echo"修改失败"; } $result=PostList::model()->updateAll(array('status'=>'1'),'staff_id=:staff and host_id=:host',array(':staff'=>$staff_id,':host'=>$host_id))
Post::model()->updateByPk($pk,$attributes,$condition,$params); $count=Admin::model()->updateByPk(1,array('username'=>'admin','password'=>'admin')); $count=Admin::model()->updateByPk(array(1,2),array('username'=>'admin','password'=>'admin'),'username=:name',array(':name'=>'admin')); if($count>0){echo"修改成功"; }else{echo"修改失败"; } Post::model()->updateCounters($counters,$condition,$params); $count=Admin::model()->updateCounters(array('status'=>1),'username=:name',array(':name'=>'admin')); if($count> 0){echo "修改成功"; }else{echo"修改失败"; }
array ('status'=>1) represents the admin table in the database. According to the condition username='admin', the status field of all query results will be incremented by 1
4. Delete data
delete
Post::model()->deleteAll($condition,$params); $count=Admin::model()->deleteAll('username=:nameandpassword=:pass',array(':name'=>'admin',':pass'=>'admin')); $count= Admin::model()->deleteAll('id in("1,2,3")');//删除id为这些的数据 if($count>0){echo"删除成功"; }else{echo"删除失败"; } Post::model()->deleteByPk($pk,$condition,$params); $count= Admin::model()->deleteByPk(1); $count=Admin::model()->deleteByPk(array(1,2),'username=:name',array(':name'=>'admin')); if($count>0){echo"删除成功"; }else{echo"删除失败"; }
5. createCommand
$sql="SELECT u.account,i.* FROM sys_user as u left join user_info as i on u.id=i.user_id"; $rows=Yii::app()->db->createCommand($sql)->query(); foreach($rowsas $k => $v){ echo$v['add_time']; }
6. Transaction processing
$dbTrans= Yii::app()->db->beginTransaction(); try{ $post=new Post; $post->'title'='Hello dodobook!!!'; if(!$post->save()){ throw new Exception("Error Processing Request", 1); } $dbTrans->commit(); $this->_end(0,'添加成功!!!'); }catch(Exception$e){ $dbTrans->rollback(); $this->_end($e->getCode(),$e->getMessage()); }
Related recommendations:
PHP lets the arrays with the same values form a new array instance explanation
The above is the detailed content of About the addition, deletion and modification of Yii framework. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

Yii framework middleware: providing multiple data storage support for applications Introduction Middleware (middleware) is an important concept in the Yii framework, which provides multiple data storage support for applications. Middleware acts like a filter, inserting custom code between an application's requests and responses. Through middleware, we can process, verify, filter requests, and then pass the processed results to the next middleware or final handler. Middleware in the Yii framework is very easy to use

With the rapid development of web applications, modern web development has become an important skill. Many frameworks and tools are available for developing efficient web applications, among which the Yii framework is a very popular framework. Yii is a high-performance, component-based PHP framework that uses the latest design patterns and technologies, provides powerful tools and components, and is ideal for building complex web applications. In this article, we will discuss how to use Yii framework to build web applications. Install Yii framework first,

Steps to implement web page caching and page chunking using the Yii framework Introduction: During the web development process, in order to improve the performance and user experience of the website, it is often necessary to cache and chunk the page. The Yii framework provides powerful caching and layout functions, which can help developers quickly implement web page caching and page chunking. This article will introduce how to use the Yii framework to implement web page caching and page chunking. 1. Turn on web page caching. In the Yii framework, web page caching can be turned on through the configuration file. Open the main configuration file co

In recent years, with the rapid development of the game industry, more and more players have begun to look for game strategies to help them pass the game. Therefore, creating a game guide website can make it easier for players to obtain game guides, and at the same time, it can also provide players with a better gaming experience. When creating such a website, we can use the Yii framework for development. The Yii framework is a web application development framework based on the PHP programming language. It has the characteristics of high efficiency, security, and strong scalability, and can help us create a game guide more quickly and efficiently.

Yii framework middleware: Add logging and debugging capabilities to applications [Introduction] When developing web applications, we usually need to add some additional features to improve the performance and stability of the application. The Yii framework provides the concept of middleware that enables us to perform some additional tasks before and after the application handles the request. This article will introduce how to use the middleware function of the Yii framework to implement logging and debugging functions. [What is middleware] Middleware refers to the processing of requests and responses before and after the application processes the request.

The JavaList interface is one of the commonly used data structures in Java, which can easily implement data addition, deletion, modification and query operations. This article will use an example to demonstrate how to use the JavaList interface to implement data addition, deletion, modification and query operations. First, we need to introduce the implementation class of the List interface in the code, the common ones are ArrayList and LinkedList. Both classes implement the List interface and have similar functions but different underlying implementations. ArrayList is based on array real

In the Yii framework, controllers play an important role in processing requests. In addition to handling regular page requests, controllers can also be used to handle Ajax requests. This article will introduce how to handle Ajax requests in the Yii framework and provide code examples. In the Yii framework, processing Ajax requests can be carried out through the following steps: The first step is to create a controller (Controller) class. You can inherit the basic controller class yiiwebCo provided by the Yii framework
