add($data); Continuous operation $User = M("User"); // Instantiate User object $data['name'] = 'ThinkPHP';$data['email']"/> add($data); Continuous operation $User = M("User"); // Instantiate User object $data['name'] = 'ThinkPHP';$data['email']">

Home  >  Article  >  Backend Development  >  About ThinkPHP 5. Some basic operations of database

About ThinkPHP 5. Some basic operations of database

jacklove
jackloveOriginal
2018-06-15 11:24:181769browse

New

Normal new

$User = M("User"); // 实例化User对象$data['name'] = 'ThinkPHP';$data['email'] = 'ThinkPHP@gmail.com';$User->add($data);

Continuous operation

$User = M("User"); // 实例化User对象$data['name'] = 'ThinkPHP';$data['email'] = 'ThinkPHP@gmail.com';$User->data($data)->add();

Use Create

$User = M("User"); // 实例化User对象
 // 根据表单提交的POST数据创建数据对象$User->create();$User->add(); // 根据条件保存修改的数据

Batch insert data

$User->addAll($data)

Read Take the

select method

$User = M("User"); // 实例化User对象
 // 查找status值为1的用户数据 以创建时间排序 返回10条数据$list = $User->where('status=1')->order('create_time')->limit(10)->select();

array as the query condition

$User = M("User"); // 实例化User对象$condition['name'] = 'thinkphp';$condition['status'] = 1; // 把查询条件传入查询方法$User->where($condition)->select();

Change the logical relationship of multiple conditions

$User = M("User"); // 实例化User对象$condition['name'] = 'thinkphp';$condition['account'] = 'thinkphp';$condition['_logic'] = 'OR'; // 把查询条件传入查询方法$User->where($condition)->select();

Object mode query

$User = M("User"); // 实例化User对象
 // 定义查询条件$condition = new stdClass(); 
$condition->name = 'thinkphp'; 
$condition->status= 1; 
$User->where($condition)->select();

Expression query

$User = M("User"); // 实例化User对象
 // 要修改的数据对象属性赋值$data['name'] = 'ThinkPHP';$data['score'] = array('exp','score+1');// 用户的积分加1$User->where('id=5')->save($data); // 根据条件保存修改的数据

Quick or query

$User = M("User"); // 实例化User对象$map['name|title'] = 'thinkphp'; // 把查询条件传入查询方法$User->where($map)->select();

Quick multi-field and query

$User = M("User"); // 实例化User对象$map['status&title'] =array('1','thinkphp','_multi'=>true); // 把查询条件传入查询方法$User->where($map)->select();

Interval query

$map['id'] = array(array('gt',1),array('lt',10)) ;

find method

$User = M("User"); // 实例化User对象
 // 查找status值为1name值为think的用户数据 $User->where('status=1 AND name="think"')->find();

Limit field

$User = M("User"); // 实例化User对象
 // 获取ID为3的用户的昵称 $nickname = $User->where('id=3')->getField('nickname');

Return an array composed of one field

$this->getField('id',true); // 获取id数组

Multiple fields return an associative array

$User = M("User"); // 实例化User对象
 // 获取所有用户的ID和昵称列表 $list = $User->getField('id,nickname');

Limit the number of returns

$this->getField('id,name',5); // 限制返回5条记录

left join

$Model->join(' work ON artist.id = work.artist_id')->join('card ON artist.card_id = card.id')->select();$Model->join('RIGHT JOIN work ON artist.id = work.artist_id')->select();

More query methods can be found here:
http://doc.thinkphp.cn/manual/query.html

Update

Update after condition selection

$User = M("User"); // 实例化User对象
 // 要修改的数据对象属性赋值$data['name'] = 'ThinkPHP';$data['email'] = 'ThinkPHP@gmail.com';$User->where('id=5')->save($data); // 根据条件保存修改的数据

Continuous operation

$User = M("User"); // 实例化User对象
 // 要修改的数据对象属性赋值$data['name'] = 'ThinkPHP';$data['email'] = 'ThinkPHP@gmail.com';//更新时间字段$data['time']=date("Y-m-d H:i:s", time());$User->where('id=5')->data($data)->save(); // 根据条件保存修改的数据

Update specific fields

$User = M("User"); // 实例化User对象
 // 更改用户的name值$User-> where('id=5')->setField('name','ThinkPHP');

Update multiple fields

$User = M("User"); // 实例化User对象
 // 更改用户的name和email的值$data = array('name'=>'ThinkPHP','email'=>'ThinkPHP@gmail.com');$User-> where('id=5')->setField($data);

Increase and decrease

$User = M("User"); // 实例化User对象$User->where('id=5')->setInc('score',3); // 用户的积分加3$User->where('id=5')->setInc('score'); // 用户的积分加1$User->where('id=5')->setDec('score',5); // 用户的积分减5$User->where('id=5')->setDec('score'); // 用户的积分减1

Delete

Conditional deletion

$User = M("User"); // 实例化User对象$User->where('id=5')->delete(); // 删除id为5的用户数据$User->where('status=0')->delete(); // 删除所有状态为0的用户数据

Limit the number of deletions

$User->where('status=0')->order('create_time')->limit('5')->delete();

Transaction

启动事务:$User->startTrans(); 
 提交事务:$User->commit();
 事务回滚:$User->rollback();

This article explains some basic operations about ThinkPHP 5. Database. For more related content, please pay attention to php Chinese website .

Related recommendations:

Rules for adding different data in thinkphp5

Related operations on ThinkPHP5 database

About ThinkPHP5 database and model usage

The above is the detailed content of About ThinkPHP 5. Some basic operations of database. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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