CURD operation of yii

不言
Release: 2023-03-24 14:30:01
Original
1490 people have browsed it

This article mainly introduces Yii's CURD operation skills, and analyzes in detail the specific usage of addition, deletion, modification and query based on Yii framework in the form of examples. It is of great practical value. Friends in need can refer to this article

The example describes Yii's CURD operation skills. Share it with everyone for your reference. The specific analysis is as follows:

CURD is an abbreviation in database technology. The basic functions of various parameters in general project development are CURD. It represents Create, Update, Retrieve and Delete operations. This article talks about the CURD operation of Yii framework.

1. Query data collection

1.

Copy code The code is as follows:

$admin=Admin::model()->findAll($condition,$params);
Copy after login

The The method is to query a collection based on a condition, such as:

Copy code The code is as follows:

findAll('username=:name',array(':name'=>$username));
Copy after login

2,

Copy code The code is as follows:

$admin=Admin::model()->findAllByPk($postIDs,$condition,$params);
findAllByPk($id,'name like ':name' and age=:age',array(':name'=>$name,'age'=>$age));
Copy after login

This method is to query a collection based on the primary key. Multiple primary keys can be used, such as:

Copy code The code is as follows:

findAllByPk(array(1,2));
Copy after login

3,

Copy code The code is as follows:

$admin=Admin::model()-> findAllByAttributes($attributes,$condition,$params);
Copy after login

This method is to query a collection based on conditions, which can be multiple conditions, and put the conditions into an array. For example:

Copy code The code is as follows:

findAllByAttributes(array('username'=>'admin'));
Copy after login

4、

Copy code The code is as follows:

$admin=Admin::model()->findAllBySql($sql,$params);
Copy after login

This method is to query an array based on the SQL statement, such as:

Copy code The code is as follows:

findAllBySql('select *from admin whereusername=:name',array(':name'=>'admin'));
Copy after login

2. Method of querying objects

1,

Copy code The code is as follows:

$admin=Admin::model()->findByPk($postID,$condition,$params);
Copy after login

Query an object based on the primary key, such as: findByPk(1);

2,

Copy code The code is as follows:

$row=Admin::model()->find($condition,$params);
Copy after login

Query a set of data based on a condition, there may be multiple, but it only returns the first row Data, such as:

Copy code The code is as follows:

find('username=:name',array(':name'=>'admin'));
Copy after login

3,

Copy code The code is as follows:

$admin=Admin::model()-> findByAttributes($attributes,$condition,$params);
Copy after login

This method is to query a set of data based on conditions, which can be multiple conditions. Put the conditions into the array, and it also queries the first piece of data, such as:

Copy code The code is as follows:

findByAttributes(array('username'=>'admin'));
Copy after login

4,

Copy code The code is as follows:

$admin=Admin::model()->findBySql($sql,$params);
Copy after login

This method is to query a set of data based on SQL statements. The query is also the first piece of data, such as:

Copy code The code is as follows:

findBySql('select *from admin whereusername=:name',array(':name'=>'admin'));
Copy after login

5. Put together a method to obtain SQL, and query an object based on find

Copy code The 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
Copy after login

3. Query the number and determine whether the query has results

1,

Copy code The code is as follows:

$n=Post::model()->count($condition,$params);
Copy after login

This method is to query how many records a collection has based on a condition and return an int type number, such as

Copy code The code is as follows:

count('username=:name',array(':name'=>$username));
Copy after login

2,

Copy code The code is as follows:

$n=Post::model()->countBySql($sql,$params);
Copy after login

This method is based on the SQL statement Query how many records there are in a collection and return an int number, such as

Copy code The code is as follows:

countBySql('select *from admin whereusername=:name',array(':name'=>'admin'));
Copy after login

3,

Copy the code The code is as follows:

$exists=Post::model()->exists($condition,$params);
Copy after login

This method is to query whether the array obtained has data according to a condition. If there is data, it returns a true, otherwise it is not found

four , Add method


##Copy code The code is as follows:

$admin=newAdmin;
$admin->username=$username;
$admin->password=$password;
if($admin->save()>0){
echo '添加成功';
}else{
echo '添加失败';
}
Copy after login

5. Modification method

1、

Copy code The code is as follows:

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 '修改失败';
}
Copy after login

2、

Copy code Code As follows:

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 '修改失败';
}
Copy after login

$pk represents the primary key, which can be one or a set, $attributes represents the set of fields to be modified, $condition represents the condition, and the value passed in by $params

3.

Copy code The 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 '修改成功';
}else{
echo '修改失败';
}
array('status'=& gt;1)代表数据库中的admin表根据条件username='admin',查询出的所有结果status字段都自加1
Copy after login

6. Deletion method

1.

复制代码 代码如下:

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.')');删除id为这些的数据
if($count>0){
echo '删除成功';
}else{
echo '删除失败';
}
Copy after login

2、

复制代码 代码如下:

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 '删除失败';
}
Copy after login

相关推荐:

yii2 resetful 的授权验证

Yii2.0 PHP使用Sphinx


The above is the detailed content of CURD operation of yii. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template