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

About the addition, deletion and modification of Yii framework

不言
Release: 2023-04-02 10:38:02
Original
2276 people have browsed it

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!

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