• YII数据库操作(CURD操作)


    数据库操作

    获得模型对象

    $model = 模型名::model();或$model = new 模型名();

    1、查询多条记录(返回值:二维数组)

    $result = $model->findAll();

    $result = $model->findAll(条件);

    $result = $model->findAll(array("select"=>"字段,字段,字段..."));//限制查询显示的字段

    $result = $model->findAll(array("select"=>"字段,字段,字段...","condition"=>"条件"));//限制查询显示的字段与条件

    $result = $model->findAll(array("select"=>"字段,字段,字段...","condition"=>"条件","order"=>"字段 desc"));//限制查询显示的字段、条件、排序

    $result = $model->findAll(array("select"=>"字段,字段,字段...","condition"=>"条件","order"=>"字段 desc","limit"=>条数,"offset"=>起始值));//限制查询显示的字段、条件、排序、limit、offset

    $result = $model->findAllBySql("select语句");

    2、查询一条记录(返回值:一维数组)

    $model = 模型名::model();或$model = new 模型名();

    $result = $model->find();

    $result = $model->find(条件);

    $result = $model->findByPK(主键);

    $result = $model->findByPK(array(主键1,主键2...));

    $result = $model->findBySql("select语句");

    3、添加记录(返回值:受影响的行数)

    $model = new 模型名();

    $model->字段 = 值;

    $model->字段 = 值;

    $model->字段 = 值;

    $result = $model->save();

    4、删除记录(返回值:受影响的行数)

    $model = 模型名::model();或$model = new 模型名();

    $result = $model->deleteAll("条件");

    $result = $model->deleteByPK(主键值);

    $result = $model->deleteByPK(array(主键1,主键2...));

    5、修改记录(返回值:受影响的行数)

    $model = 模型名::model();或$model = new 模型名();

    $result = $model->updateAll(关联数组,条件);

    $result = $model->updateByPK(主键,关联数组);

    $result = $model->updateByPK(array(主键1,主键2...),关联数组);

    6、执行sql语句(返回值:二维数组)

    $db = Yii::app()->db;

    $st = $db->createCommand("select查询语句");

    $rs = $st->queryAll();//获得多条记录

    7、执行sql语句(返回值:一维数组)

    $db = Yii::app()->db;

    $st = $db->createCommand("select查询语句");

    $rs = $st->queryRow();//获得一条记录

    8、执行sql语句(返回值:一维数组)

    $db = Yii::app()->db;

    $st = $db->createCommand("select查询语句");

    $rs = $st->queryColumn();//获得第一个字段的所有记录

    9、执行sql语句(返回值:二维数组)

    $db = Yii::app()->db;

    $st = $db->createCommand("select查询语句");

    $obj = $st->query();

    $rs = $st->readAll();//获得多条记录

    10、执行sql语句(返回值:一维关联数组)

    $db = Yii::app()->db;

    $st = $db->createCommand("select查询语句");

    $obj = $st->query();

    $rs = $obj->read();//获得一条记录

    11、执行sql语句(返回值:受影响的行数)

    $db = Yii::app()->db;

    $st = $db->createCommand("insert、update、delete语句");

    $rs = $st->execute();//执行insert、update、delete语句

    12、执行sql语句,绑定参数(返回值:成功还是失败)

    $db = Yii::app()->db;

    $st = $db->createCommand("insert、update、delete语句");

    $st->bindParam(":名",$变量,PDO::PARAM_STR);

    $st->bindParam(":名",$变量,PDO::PARAM_STR);

    $st->bindParam(":名",$变量,PDO::PARAM_STR);

    $rs = $st->execute();//执行insert、update、delete语句

    13、获得查询到的记录数(返回值:数字值)

    $model = 模型名::model();或$model = new 模型名();

    $totalRow = $model->count();

    $totalRow = $model->count(条件);

    14、通过CDbCriteria类来实现查询操作(返回值:二维数组)

    位置:framework/db/schema/CDbCriteria.php

    $model = new Model();或$model = 类名::model()

    $criteria = new CDbCriteria();

    $criteria->select = "字段,字段,字段|*";

    $criteria->condition = "条件";

    $criteria->order = "排序";

    $criteria->limit = 条数;

    $criteria->offset = 起始值;

    $result = $model->findAll($criteria);

  • 相关阅读:
    福大软工 · 第十次作业
    最终作业:软工实践个人总结
    第三视角Beta答辩总结
    Beta 冲刺(7/7)
    Beta 冲刺 (6/7)
    Beta 冲刺 (5/7)
    Beta 冲刺 (4/7)
    Beta 冲刺 (3/7)
    Beta 冲刺 (2/7)
    福大软工 · 第十次作业
  • 原文地址:https://www.cnblogs.com/zhengyanbin2016/p/5388145.html
Copyright © 2020-2023  润新知