zoukankan      html  css  js  c++  java
  • yii的增删改查

    要想实现数据库的操作,必须首先初始化数据模型。

    一、yii的数据库模型的初始化有两种。第一种是利用模型的静态方法model;第二种是实例化一个新的模型。以例子说明:

                //数据的查询关键代码
                $goods_model = Goods::model();
                $goods_infos = $goods_model -> findAll();
                
                //数据的添加关键代码
                $goods_model = new Goods();
                $goods_model ->goods_name = 'xiaoming';
                $goods_model ->save();
    
                //数据的修改关键代码
                $goods_model = Goods::model();
                $goods_info =  $goods_model -> findByPk($id);
                $goods_info ->goods_name = 'xiaozhang';
                $goods_info -> save();//这里注意用的是原有的数据对象($goods_info),而添加时用的是新的数据模型对象($goods_model);
               
                 //数据的删除关键代码
                 $goods_model = Goods::model();
                 $goods_info = $goods_model -> findByPk($id);
                 $goods_info -> delete(); 

    这里初始化数据模型的时候,只有添加数据的时候用到了new Goods();其他时候都是用Goods::model();

    另外删除和修改 都是针对具体的某个对象数据进行的操作(这里不同于TP的操作)。

    二、数据查找的延伸

              /**
                 * 查询的具体条件设置可以用findAll
                 * 想要查询具体的 '字段' 用‘select’;
                 * 想要查询具体的 '条件' 用‘condition’
                 * 想要查询具体的 '排序' 用‘order’
                 * 想要查询具体的 '分组' 用‘group’
                 * 想要查询具体的 '限制' 用‘limit’
                 * 想要查询具体的 '偏移量' 用‘offset’
                 */
                $goods_infos = $goods_model -> findAll(array(
                    'select'        =>  'goods_name , id',
                    'condition'     =>  'id < 5',
                    'order'         =>  'id desc',
                    'group'         =>  'goods_category',
                    'limit'         =>  '3',
                    'offset'        =>  '1',
                ));
  • 相关阅读:
    Serializable读写类操作
    socket模拟通信
    使用poi实现生成excel文件
    注解形式的通知
    使用linkedhashmap实现LRU(最近最少使用缓存算法)
    websocket的使用
    centos systemctl daemon-reload 提示 no such file or directory 的一个原因
    mac 10.13 build 一个 redis desktop manager
    mac 必备工具
    supervisor 添加新配置不生效的问题
  • 原文地址:https://www.cnblogs.com/scrit/p/3770832.html
Copyright © 2011-2022 走看看