In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the introduction of CURD operation skills of yii framework". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "introduction to CURD operation skills of yii framework".
This paper gives an example to describe the CURD operation skills of yii. Share it with you for your reference. The specific analysis is as follows:
CURD is an acronym in database technology. The basic function of various parameters developed by a general project is CURD. It represents create (Create), update (Update), read (Retrieve), and delete (Delete) operations. This article will talk about the CURD operation of the Yii framework.
1. Query data set
1 、
The copy code is as follows:
$admin=Admin::model ()-> findAll ($condition,$params)
This method queries a collection according to a condition, such as:
The copy code is as follows:
FindAll ('username=:name',array (': name'= > $username))
2 、
The copy code is as follows:
$admin=Admin::model ()-> findAllByPk ($postIDs,$condition,$params)
FindAllByPk ($id,'name like': name' and age=:age',array (': name'= > $name,'age'= > $age); this method queries a collection based on the primary key, and can use multiple primary keys, such as:
The copy code is as follows:
FindAllByPk (array (1pm 2))
3 、
The copy code is as follows:
$admin=Admin::model ()-> findAllByAttributes ($attributes,$condition,$params)
This method queries a collection according to conditions, which can be multiple conditions, and puts the conditions into an array, such as:
The copy code is as follows:
FindAllByAttributes (array ('username'= >' admin'))
4 、
The copy code is as follows:
$admin=Admin::model ()-> findAllBySql ($sql,$params)
This method queries an array based on the SQL statement, such as:
The copy code is as follows:
FindAllBySql ('select * from admin whereusername=:name',array (': name'= > 'admin'))
Second, the method of querying the object
1 、
The copy code is as follows:
$admin=Admin::model ()-> findByPk ($postID,$condition,$params)
Query an object according to the primary key, such as: findByPk (1)
2 、
The copy code is as follows:
$row=Admin::model ()-> find ($condition,$params)
Query a set of data according to a condition, possibly multiple, but it only returns the first row of data, such as:
The copy code is as follows:
Find ('username=:name',array (': name'= > 'admin'))
3 、
The copy code is as follows:
$admin=Admin::model ()-> findByAttributes ($attributes,$condition,$params)
This method is to query a set of data according to conditions, which can be multiple conditions, and put the conditions into an array, and he also queries the first piece of data, such as:
The copy code is as follows:
FindByAttributes (array ('username'= >' admin'))
4 、
The copy code is as follows:
$admin=Admin::model ()-> findBySql ($sql,$params)
This method queries a set of data according to the SQL statement, and it also queries the first piece of data, such as:
The copy code is as follows:
FindBySql ('select * from admin whereusername=:name',array (': name'= > 'admin'))
5. Spell out a method to obtain SQL and query an object according to find
The copy code is as follows:
$criteria=new CDbCriteria
$criteria- > select='username'; / / only select the 'title' column
$criteria- > condition='username=:username'
$criteria- > params=array (': username= > 'admin')
$post=Post::model ()-> find ($criteria); / / $params isnot needed
Third, the number of queries to determine whether the query has a result
1 、
The copy code is as follows:
$n=Post::model ()-> count ($condition,$params)
This method queries how many records there are in a collection according to a condition and returns an int number, such as
The copy code is as follows:
Count ('username=:name',array (': name'= > $username))
2 、
The copy code is as follows:
$n=Post::model ()-> countBySql ($sql,$params)
This method queries how many records there are in a collection according to the SQL statement and returns an int number, such as
The copy code is as follows:
CountBySql ('select * from admin whereusername=:name',array (': name'= > 'admin'))
3 、
The copy code is as follows:
$exists=Post::model ()-> exists ($condition,$params)
This method is based on a condition to query whether there is any data in the array. If any data is returned, a true will not be found.
Fourth, the method of adding
The copy code is as follows:
$admin=newAdmin
$admin- > username=$username
$admin- > password=$password
If ($admin- > save () > 0) {
Echo 'added successfully'
} else {
Echo 'add failed'
}
V. the method of modification
1 、
The copy code is as follows:
Post::model ()-> updateAll ($attributes,$condition,$params)
$count = Admin::model ()-> updateAll (array ('username'= >' 11111), 'password=:pass',array (': pass'= > '1111a1'))
If ($count > 0) {
Echo 'modified successfully'
} else {
Echo 'failed to modify'
}
2 、
The copy code is as follows:
Post::model ()-> updateByPk ($pk,$attributes,$condition,$params)
$count = Admin::model ()-> updateByPk (1 username'= > 'admin','password'= >' admin'))
$count = Admin::model ()-> updateByPk (array (1Jue 2), array ('username'= >' admin','password'= > 'admin'),' username=:name',array (': name'= > 'admin'))
If ($count > 0) {
Echo 'modified successfully'
} else {
Echo 'failed to modify'
}
The competition represents the primary key, which can be either a collection or a collection, $attributes represents the collection of fields to be modified, $condition represents the condition, and the value passed in by $params
3 、
The copy code is as follows:
Post::model ()-> updateCounters ($counters,$condition,$params)
$count=Admin::model ()-> updateCounters (array ('status'= > 1),' username=:name',array (': name'= > 'admin'))
If ($count > 0) {
Echo 'modified successfully'
} else {
Echo 'failed to modify'
}
Array ('status'=& gt;1) represents the admin table in the database. All the result status fields queried by conditional username='admin', are incremented by 1.
VI. Method of deletion
1 、
The copy code is as follows:
Post::model ()-> deleteAll ($condition,$params)
$count = Admin::model ()-> deleteAll ('username=:nameandpassword=:pass',array (': name'= > 'admin',':pass'= >' admin'))
$id=1,2,3
DeleteAll ('id in ('.$ id.')'); delete the data whose id is these
If ($count > 0) {
Echo 'deleted successfully'
} else {
Echo 'deletion failed'
}
2 、
The copy code is as follows:
Post::model ()-> deleteByPk ($pk,$condition,$params)
$count = Admin::model ()-> deleteByPk (1)
$count = Admin::model ()-> deleteByPk (array (1), 'username=:name',array (': name'= > 'admin'))
If ($count > 0) {
Echo 'deleted successfully'
} else {
Echo 'deletion failed'
}
At this point, I believe that you have a deeper understanding of the "introduction to CURD operation skills of the yii framework". You might as well come to the actual operation. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.