Home Backend Development PHP Tutorial About the addition, deletion and modification of Yii framework

About the addition, deletion and modification of Yii framework

Jul 04, 2018 pm 01:49 PM

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 ");
Copy after login

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));
Copy after login

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'));
Copy after login

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%'));
Copy after login

5. findByPk(Query an object based on the primary key)

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

$admin=Admin::model()->findByPk(1);
Copy after login

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'));
Copy after login

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'));
Copy after login

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'));
Copy after login

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));
Copy after login

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'));
Copy after login

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));
Copy after login

2. Add data

save(add data)

$admin=new Admin;       

$admin->username =$username;

$admin->password =$password;

if($admin->save() > 0){echo"添加成功"; }else{echo"添加失败"; }
Copy after login

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

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

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'];

}
Copy after login

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());

}
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

PHP lets the arrays with the same values ​​form a new array instance explanation

Detailed explanation of solving the problem of inconsistent PHP string lengths

Laravel5.2 uses Captcha to generate verification codes to achieve login

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does Go language implement the addition, deletion, modification and query operations of the database? How does Go language implement the addition, deletion, modification and query operations of the database? Mar 27, 2024 pm 09:39 PM

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.

RESTful API development in Yii framework RESTful API development in Yii framework Jun 21, 2023 pm 12:34 PM

Yii is a high-performance MVC framework based on PHP. It provides a very rich set of tools and functions to support the rapid and efficient development of web applications. Among them, the RESTful API function of the Yii framework has attracted more and more attention and love from developers, because using the Yii framework can easily build high-performance and easily scalable RESTful interfaces, providing strong support for the development of web applications. . Introduction to RESTfulAPI RESTfulAPI is a

Yii framework middleware: providing multiple data storage support for applications Yii framework middleware: providing multiple data storage support for applications Jul 28, 2023 pm 12:43 PM

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

How to use Yii framework in PHP How to use Yii framework in PHP Jun 27, 2023 pm 07:00 PM

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 Yii framework Steps to implement web page caching and page chunking using Yii framework Jul 30, 2023 am 09:22 AM

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

Create a game guide website using Yii framework Create a game guide website using Yii framework Jun 21, 2023 pm 01:45 PM

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 your application Yii Framework Middleware: Add logging and debugging capabilities to your application Jul 28, 2023 pm 08:49 PM

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.

Java List Interface Example Demonstration: Data Operation to Implement Add, Delete, Modify and Check Operations Java List Interface Example Demonstration: Data Operation to Implement Add, Delete, Modify and Check Operations Dec 20, 2023 am 08:10 AM

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

See all articles