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);
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));
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));
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));
3,
Copy code The code is as follows:
$admin=Admin::model()-> findAllByAttributes($attributes,$condition,$params);
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'));
4、
Copy code The code is as follows:
$admin=Admin::model()->findAllBySql($sql,$params);
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'));
2. Method of querying objects
1,
Copy code The code is as follows:
$admin=Admin::model()->findByPk($postID,$condition,$params);
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);
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'));
3,
Copy code The code is as follows:
$admin=Admin::model()-> findByAttributes($attributes,$condition,$params);
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'));
4,
Copy code The code is as follows:
$admin=Admin::model()->findBySql($sql,$params);
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'));
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
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);
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));
2,
Copy code The code is as follows:
$n=Post::model()->countBySql($sql,$params);
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'));
3,
Copy the code The code is as follows:
$exists=Post::model()->exists($condition,$params);
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 '添加失败'; }
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 '修改失败'; }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 '修改失败'; }$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 $params3.
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
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 '删除失败'; }
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 '删除失败'; }
相关推荐:
The above is the detailed content of CURD operation of yii. For more information, please follow other related articles on the PHP Chinese website!

PHP编程中有哪些常见的Behat操作?Behat是一个行为驱动开发(BDD)工具,允许测试人员和开发人员在自然语言中撰写测试用例,并将这些用例转化为可执行的代码。它支持PHP语言,并提供了丰富的库和功能,可以实现多种常见的测试操作。下面列举了PHP编程中常见的Behat操作。前置条件(Background)在编写测试用例时,有时候会有一些公共的前置条件需要

ThinkPHP6是一款基于PHP的MVC框架,极大地简化了Web应用程序的开发。其中表单验证是一个非常基础和重要的功能。在这篇文章中,我们将介绍ThinkPHP6中如何进行表单验证操作。一、验证规则定义在ThinkPHP6中,验证规则都需要定义在控制器中,我们可以通过在控制器中定义一个$validate属性来实现规则的定义,如下所示:usethinkVa

PHP编程中有哪些常见的jQuery操作?在PHP编程中,使用jQuery进行网页开发是一种非常方便和高效的方式。jQuery是一个简单而强大的JavaScript库,包含了许多实用的方法和函数。在PHP编程中,我们常常使用jQuery来操纵HTML和DOM元素,使网页具有更好的交互性和高度的可视化效果。在本文中,我们将介绍一些常见的PHP编程中使用jQue

OAuth(开放授权)是一种用于授权访问控制的标准化协议。在Web开发中,使用OAuth可以帮助应用程序安全地从第三方平台中获取用户数据或资源。而在PHP编程中,OAuth操作也被广泛应用。本文将介绍PHP编程中的常见OAuth操作。OAuth1.0a授权OAuth1.0a授权是OAuth中最早出现的授权方式,也是最复杂的一种授权方式。在PHP编程中,O

随着全球化的发展,越来越多的网站和应用程序需要提供多语言支持。而对于使用ThinkPHP6框架的开发者来说,如何实现多语言翻译操作是一个重要的需求。本文将介绍怎样使用ThinkPHP6进行多语言翻译操作。配置语言包在ThinkPHP6中,语言包是一个包含键值对的数组。可以将其存储在app/lang/目录下的各种子目录中。例如:/app/lang/zh-cn/

随着Web应用程序的发展,Ajax成为了一种重要的技术,在PHP编程中也得到了广泛的应用。通过Ajax技术,Web应用程序可以实现异步操作,从而提高了用户体验和应用程序性能。在本文中,我们将探讨PHP编程中常见的Ajax操作。一、Ajax基础知识在介绍常见的Ajax操作之前,我们先来了解一下Ajax技术的基础知识。Ajax全称为"AsynchronousJ

随着互联网的快速发展,基于图形的验证码已经成为了网站安全保障的一个重要环节。验证码可以有效地防止机器人或恶意程序对网站进行自动化操作,同时也可以保障用户信息的安全性。而在基于ThinkPHP6的网站开发中,如何实现captcha图形验证码的操作呢?本文将为您介绍具体的操作流程。一、生成Captcha图形验证码1、使用captcha库进行安装在ThinkPHP

随着互联网应用的不断发展,搜索引擎也成为了日常生活中必不可少的工具,而分词搜索是搜索引擎中非常重要的一种搜索方式。在使用ThinkPHP6框架开发项目时,我们也需要对分词搜索进行深入了解和应用。本文将介绍ThinkPHP6中如何进行分词搜索操作。一、分词搜索简介分词搜索是将用户输入的关键词进行分割,然后在数据库中进行模糊搜索,找到相符合的记录。相较于传统的搜


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Chinese version
Chinese version, very easy to use

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

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