Model:
have how many Data Sheet , you build how much Model
The model is actually class
we operate on the database, we need to instantiate the model class, generate Object
called by the object to invoke the associated Method , you can implement the operation of the database
Add record
- 1 [php] 2 $post = newpost (); 3 $post ->title = "Jack ; 4 $post ->content = "1111111" Span style= "color: #000000;" >; 5 $post ->createtime = time (); 6 $post ->save ();
By observing the MDM project, I found that most of the insert operations in model use the DAO method, the native Insert SQL statement, which can be done in model:
-
1 Public functionInsert$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",$namePdo::param_str); 8 $cmd->bindparam (": Createtime",$timePdo::param_str); 9 $cmd-execute ();Ten}
or inThe controller uses a lot of AR (more operations are performed as follows in the controllers, using the AR class's Save () method): The premise is, in the model, define the Rule,rule how to define, and for the time being not clear The official gives the following reasons:We can use the Attributes property to collect form information uniformly, but the data table constraint rules must be complete. And you need to set the validation rules to safe, otherwise the information cannot be saved by attributes
-
1 Public functionActioninsert ()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){
Ten Echo' Success ';
One}Else{
A Echo' Fail ';
- }
-}
Update records are generally also updated in the controller,
-
1 Public functionactionupdate ()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=:p id",Array(":p id" =$id)); 9}
FINDBYPK () uses a non-instantiated object to manipulate the database in the following way: Managementendgroup::model ()->find ();
1 Public functionactionmodify ()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;Ten $manage= Managementendgroup::model ()->findbypk ($manageData-GroupID); One $manage->attributes =$datarry;
A $saved=$manage-save (); - $rslt=NewStdClass (); - $rslt->code =$saved? 0:-1;
the Print_r(Json_encode ($rslt)); -}
goods.php (model)
-
-
1 /**
2 * Check Product list (array mode)3 */
4 Public functiongoodslist ()5 {
6 $sql= "SELECT * from Sw_goods where 1=1";
7 $result=$this->findallbysql ($sql); 8
9 $resultarray=Array();
Ten foreach($result as $item)
One {
A $itemarray=Array();
- $itemarray["goods_id"] =$item["goods_id"]; - $itemarray["goods_name"] =$item["Goods_name"]; the $itemarray["goods_weight"] =$item["Goods_weight"]; - $itemarray["goods_price"] =$item["Goods_price"]; - $itemarray["goods_number"] =$item["Goods_number"]; - $itemarray["goods_category_id"] =$item["goods_category_id"]; + $itemarray["goods_brand_id"] =$item["Goods_introduce"]; - $itemarray["goods_big_img"] =$item["Goods_big_img"]; + $itemarray["goods_small_img"] =$item["Goods_small_img"]; A $itemarray["goods_create_time"] =$item["Goods_create_time"]; at $resultarray[]=$itemarray;
- }
- return $resultarray;
- //Print_r ($resultarray);
-}
goodscontroller.php (Controller)
-
1 //Product List Page
2 publicfunction actioncategory ()3 {
4 //Render () with layout rendering5 //renderpartial () Partial rendering
6 $goods=newgoods ();7 $goodslist=$goods-goodslist ();8 Print_r($goodslist); 9 $this->render (' Category '); Ten}
From for notes (Wiz)
Yii Operations Database (AR)