zoukankan      html  css  js  c++  java
  • yii2 框架的 AR 和 DAO 增删改查

    自己做个总结  方便以后查找使用

    /**
    * yii 的增删改查
    */
    //增
    public function add1($data)
    {
    $data = [
    'title'=>$data['YiiNews']['title'],
    'msg'=>$data['YiiNews']['msg'],
    'img'=>$data['News']['img'],
    ];
    $this->setAttributes($data) ;
    return $this->insert();
    }

    public function add2($data)
    {
    $this->setAttributes($data);
    $this->isNewRecord = true;
    $this->id = 0;
    return $this->save(0);
    }

    public function add3($data)
    {
    $sql = "insert into yii_news (title,msg) values ('{$data['title']}','{$data['msg']}')";
    return yii::$app->db->createCommand($sql)->execute();
    }

    //删
    public function del1($id)
    {
    $this->deleteAll('id = '.$id);//作死写法 不要这么写
    $this->deleteAll(['id'=>$id]);
    }

    public function del2($id)
    {
    $sql = "delete from yii_news where id = '$id'";
    return yii::$app->db->createCommand($sql)->execute();
    }

    //改
    public function save1($data)
    {
    return $this->updateAll($data,['id'=> 1]);
    }

    //查
    public function sel1()
    {
    return $this->find()->where("title = 'title11'")->andWhere("id > 0")->asArray(true)->all();
    // return $this->find()->select()->where("title = 'title11'")->orWhere("id = 9")->asArray(true)->one();
    }

    public function sel2()
    {
    $sql = "select * from yii_news ";
    return yii::$app->db->createCommand($sql)->queryAll();
    }

    更具体欢迎访问 http://www.php20.com/forum.php?mod=viewthread&tid=158&extra=page%3D1

    既不回头,何必不忘; 既然无缘,何须誓言; 今日种种,逝水无痕; 明夕何夕,君已陌路;
  • 相关阅读:
    Schema约束
    gitalk报错问题
    SQL语句中单引号、双引号和反引号的区分
    用Eclipse上传项目到github
    git服务器搭建
    使用IntelliJ IDEA和Eclipse导入Github项目
    事务隔离级别的简单理解
    大公司里怎样开发和部署前端代码?
    页面无刷新Upload File
    MVC 文件上传问题
  • 原文地址:https://www.cnblogs.com/zyjfire/p/6849054.html
Copyright © 2011-2022 走看看