首页 > 代码库 > yii框架的增删改查

yii框架的增删改查

加载对象: 在控制器中 use yii\db\Query;
$query=new Query();
查询多条:$arr=$query->select(‘*‘)->from(‘register‘)->where("username=‘jiahaiyong‘")->all();
查询单条:$arr=$query->select(‘*‘)->from(‘register‘)->where("username=‘jiahaiyong‘")->one();
限制条数:$query->limit(10);
两表联查:$query->select([‘user.name AS author‘, ‘post.title as title‘])->from(‘user‘)->leftJoin(‘post‘, ‘post.user_id = user.id‘);
修改:eg: $res=Yii::$app-db->createCommand()->update(‘表名‘, 修改数组, " id = $id ")->execute();
删除:eg:$res=Yii::$app->db->createCommand()->delete(‘表名‘, "id = $id条件")->execute();
添加eg:$res= Yii::$app->db->createCommand()->insert(‘表名‘,插入的数组)->execute();

yii框架的增删改查