Fork me on GitHub

yii操作数据库(AR)

模型:

有多少数据表,就建立多少模型

模型其实就是

我们对数据库进行操作,需要实例化模型类,产生对象

通过对象调用相关的方法,就可以实现数据库的操作

 
增加记录
  1. 1 [php]
    2 $post =newPost();
    3 $post->title ="jack";
    4 $post->content ="1111111";
    5 $post->createtime = time();
    6 $post->save();
 
通过观察mdm项目发现在model中大多数的insert操作使用的是DAO方式,原生的insert sql语句;
在model中可以这样:
  1.  1 public function insert($id,$name)
     2 {
     3     $con = $this->dbConnection;
     4     $sql="insert into user (username,createtime) values (:username,:createtime);";
     5     $time = date("Y-m-d H:i:s");
     6     $cmd = $con->createCommand($sql);
     7     $cmd->bindParam(":username",$name,PDO::PARAM_STR);
     8     $cmd->bindParam(":createtime",$time,PDO::PARAM_STR);
     9     $cmd->execute();
    10 }

     

或者在controller中使用AR的较多(更多的操作方式是如下在控制器中执行,使用AR类的save()方法):
前提是,要在Model中,定义好rule,rule怎么定义,暂时还没弄明白
官方给的是如下原因:
我们可以使用attributes属性统一收集表单信息,但是数据表约束规则必须完整。
并且需要设置验证规则rules为safe,否则信息无法被attributes保存
  1.  1 public function actionInsert()
     2 {
     3     $arr = array();
     4     $array["name"]= $_GET["name"];
     5     $array["time"]= $_GET["time"];
     6     $user =newManagementUser("save");
     7     $user->attributes = $arr;
     8     $saved = $user->save();
     9     if($saved){
    10         echo 'success';
    11     }else{
    12         echo 'fail';
    13     }
    14 }
更新记录
一般也是在controller中更新,
  1. 1 public function actionUpdate()
    2 {
    3     $id = $_GET["id"];
    4     $policy =newDevicePolicy("update");
    5     $parray = array();
    6     $parray["name"]='default';
    7     $parray["time"]= time();
    8     $update = $policy->updateAll($parray,"id=:pid",array(":pid"=>$id));
    9 }
findByPk()使用
不实例化对象,操作数据库的方式如下:ManagementEndGroup::model()->find();
 1 public function actionModify()
 2     {
 3         $manageData = json_decode(file_get_contents("php://input"));
 4         $datarry = array();
 5         $datarry["id"] = $manageData->groupid;
 6         $datarry["name"] = $manageData->groupname;
 7         $datarry["desc"] = $manageData->groupdesc;
 8         if(isset($manageData->upgroup))
 9             $datarry["upgroup_id"] = $manageData->upgroup;
10         $manage =  ManagementEndGroup::model()->findByPk($manageData->groupid);
11         $manage->attributes = $datarry;
12         $saved = $manage->save();
13         $rslt = new stdClass();
14         $rslt->code = $saved?0:-1;
15         print_r(json_encode($rslt));
16     }  

 

Goods.php(模型)
    1.  1   /**
       2      * 查询商品列表(数组方式)
       3      */
       4     public function GoodsList()
       5     {
       6         $sql = "select * from sw_goods where 1=1";
       7         $result = $this->findAllBySql($sql);
       8         
       9         $resultarray = array();
      10         foreach ($result as $item)
      11         {
      12             $itemarray = array();
      13             $itemarray["goods_id"] = $item["goods_id"];
      14             $itemarray["goods_name"] = $item["goods_name"];
      15             $itemarray["goods_weight"] = $item["goods_weight"];
      16             $itemarray["goods_price"] = $item["goods_price"];
      17             $itemarray["goods_number"] = $item["goods_number"];
      18             $itemarray["goods_category_id"] = $item["goods_category_id"];
      19             $itemarray["goods_brand_id"] = $item["goods_introduce"];
      20             $itemarray["goods_big_img"] = $item["goods_big_img"];
      21             $itemarray["goods_small_img"] = $item["goods_small_img"];
      22             $itemarray["goods_create_time"] = $item["goods_create_time"];
      23             $resultarray[]=$itemarray;
      24         }
      25         return $resultarray;
      26 //      print_r($resultarray);
      27     }  

       

GoodsController.php(控制器)
  1.  1 //商品列表页
     2 publicfunction actionCategory()
     3 {
     4 //render()带布局渲染
     5 //renderPartial()部分渲染
     6 $goods =newGoods();
     7 $goodslist = $goods->GoodsList();
     8 print_r($goodslist);
     9 $this->render('category');
    10 }

     



posted @ 2016-01-12 10:30  LookPHP  阅读(492)  评论(0编辑  收藏  举报