关于模型部分参考http://blog.csdn.net/buyingfei8888/article/details/40208729
控制器部分:
<?php class GoodsController extends Controller{ function actionShow(){ $goods_model = Goods::model(); //简单查询可以通过模型里面静态方法来创建 $sql = "select goods_id,goods_name,goods_price,goods_create_time from {{goods}} limit 10"; $goods_infos = $goods_model ->findAllBySql($sql);//通过原生态sql进行查询,findALlBySql返回一个对象数组, // var_dump($goods_infos); // foreach($goods_infos as $v){ // echo $v ->goods_name ."<br />"; // } // exit(); $this->render('show',array("goods_infos"=>$goods_infos)); //这种方式会渲染布局 //$this->renderPartial('add',array('goods_model' => $goods_model)); //这种方式不会渲染布局 } function actionAdd(){ $goods_model = new Goods(); if(isset($_POST['Goods'])){ // $goods_model->goods_name = 'apple phone'; // $goods_model->goods_price = '5199'; // $goods_model->goods_weight=102; //对上面代码优化 foreach($_POST['Goods'] as $_k => $_v){ $goods_model -> $_k = $_v; } } if($goods_model->save()){ $this ->redirect('./index.php?r=houtai/goods/show'); }else{ echo "error"; } // $this->renderPartial('add',array('goods_model' => $goods_model)); } /* * YII 对get优化,作为参数传递进来 * 模板和添加时模板基本一样,直接复制稍微修改就行,都是通过小物件 ,自动会把数据关联起来,节省开发成本 */ function actionUpdate($id){ //除了添加数据进行 new Goods(), 别的都是调用静态方法 调用save方法执行是insert // Goods::model()调用save方法执行是update $goods_model = Goods::model(); $goods_info = $goods_model ->findByPk($id); //$goods_info 是 数据模型对象 if(isset($_POST['Goods'])){ foreach($_POST['Goods'] as $_k => $_v){ $goods_info -> $_k = $_v; } if($goods_info -> save()){ $this ->redirect('./index.php?r=houtai/goods/show'); } } $this->renderPartial('update',array('goods_model' => $goods_info )); } function actionDel($id){ //根据$id获得被删除数据对象,通过该对象调用delete方法就ok $goods_mode = Goods::model(); $goods_info = $goods_mode ->findByPk($id); //获得被删除商品模型对象 if($goods_info ->delete() ){ $this ->redirect('./index.php?r=houtai/goods/show'); }else{ echo 'error'; } } function actionJia(){ $goods_model = new Goods(); // $goods_model=Goods::model(); $goods_model->goods_name = 'apple phone'; $goods_model->goods_price = '5199'; $goods_model->goods_weight=102; var_dump($goods_model->save()); // var_dump($goods_model); if($goods_model->save()){ echo 'success'; }else{ echo "error"; } } } ?>
部分源码:
<table class="table_a" border="1" width="100%"> <tbody><tr style="font-weight: bold;"> <td>序号</td> <td>商品名称</td> <td>库存</td> <td>价格</td> <td>图片</td> <td>缩略图</td> <td>品牌</td> <td>创建时间</td> <td align="center">操作</td> </tr> <?php $i=1; foreach($goods_infos as $_v){ ?> <tr id="product1"> <td><?php echo $i++;?></td> <td><a href="#">苹果(APPLE)iPhone 4S<?php echo $_v->goods_name;?></a></td> <td><?php echo $_v->goods_number;?></td> <td><?php echo $_v->goods_price ;?></td> <td><img src="<?PHP ECHO HOUTAI_IMG_URL ;?><?php echo $_v->goods_big_img;?>" height="60" width="60"></td> <td><img src="<?PHP ECHO HOUTAI_IMG_URL ;?><?php echo $_v->goods_small_img;?>" height="40" width="40"></td> <td><?php echo $_v->goods_brand_id;?></td> <td><?php echo $_v->goods_create_time;?></td> <td><a href="./index.php?r=houtai/goods/update&id=<?php echo $_v->goods_id;?>">修改</a></td> <!-- <td><a href="./index.php?r=houtai/goods/update&id=<?php// echo $_v->goods_id;?>&name=test">修改</a></td>--> <td><a href="./index.php?r=houtai/goods/del&id=<?php echo $_v->goods_id;?> "> 删除</a></td> </tr> <?php } ?> <tr> <td colspan="20" style="text-align: center;"> [1] </td> </tr> </tbody> </table>
<?php $form = $this -> beginWidget("CActiveForm");?> <!-- 调用了 CActiveForm 类 怎样查找CActiveForm类,在\framework\yiilite.php 里查找,这个文件10000多行,可见yii核心代码就是这么多行 通过这个在找到 CActiveForm所在文件 CActiveForm.php包含了很多方法。就是对表单元素操作 --> <table border="1" width="100%" class="table_a"> <tr> <!-- <td>商品名称</td>--> <!-- <td><input type="text" name="f_goods_name" /></td>--> <td><?php echo $form->labelEx($goods_model,'goods_name');?></td> <td><?php echo $form -> textField($goods_model,'goods_name');?></td> </tr> <tr> <td><?php echo $form->labelEx($goods_model,'goods_weight');?></td> <td> <?php echo $form -> textField($goods_model,'goods_weight');?></td> </tr> <tr> <td><?php echo $form->labelEx($goods_model,'goods_price');?></td> <td> <?php echo $form -> textField($goods_model,'goods_price');?></td> </tr> <tr> <td><?php echo $form->labelEx($goods_model,'goods_number');?></td> <td> <?php echo $form -> textField($goods_model,'goods_number');?></td> </tr> <tr> <td><?php echo $form->labelEx($goods_model,'goods_category_id');?></td> <td> <?php echo $form -> textField($goods_model,'goods_category_id');?></td> </tr> <tr> <td><?php echo $form->labelEx($goods_model,'goods_brand_id');?></td> <td><?php echo $form -> textField($goods_model,'goods_brand_id');?></td> </tr> <tr> <td><?php echo $form->labelEx($goods_model,'goods_introduce');?></td> <td><?php echo $form -> textArea($goods_model,'goods_introduce',array('cols' => 20,"rows" => 5));?></td> </tr> </table> <td colspan="2" align="center"> <input type="submit" value="添加"> </td> <?php $this->endWidget();?>修改和上面基本相同:
<?php $form = $this -> beginWidget("CActiveForm");?> <!-- 调用了 CActiveForm 类 怎样查找CActiveForm类,在\framework\yiilite.php 里查找,这个文件10000多行,可见yii核心代码就是这么多行 通过这个在找到 CActiveForm所在文件 CActiveForm.php包含了很多方法。就是对表单元素操作 --> <table border="1" width="100%" class="table_a"> <tr> <!-- <td>商品名称</td>--> <!-- <td><input type="text" name="f_goods_name" /></td>--> <td><?php echo $form->labelEx($goods_model,'goods_name');?></td> <td><?php echo $form -> textField($goods_model,'goods_name');?></td> </tr> <tr> <td><?php echo $form->labelEx($goods_model,'goods_weight');?></td> <td> <?php echo $form -> textField($goods_model,'goods_weight');?></td> </tr> <tr> <td><?php echo $form->labelEx($goods_model,'goods_price');?></td> <td> <?php echo $form -> textField($goods_model,'goods_price');?></td> </tr> <tr> <td><?php echo $form->labelEx($goods_model,'goods_number');?></td> <td> <?php echo $form -> textField($goods_model,'goods_number');?></td> </tr> <tr> <td><?php echo $form->labelEx($goods_model,'goods_category_id');?></td> <td> <?php echo $form -> textField($goods_model,'goods_category_id');?></td> </tr> <tr> <td><?php echo $form->labelEx($goods_model,'goods_brand_id');?></td> <td><?php echo $form -> textField($goods_model,'goods_brand_id');?></td> </tr> <tr> <td><?php echo $form->labelEx($goods_model,'goods_introduce');?></td> <td><?php echo $form -> textArea($goods_model,'goods_introduce',array('cols' => 20,"rows" => 5));?></td> </tr> </table> <td colspan="2" align="center"> <input type="submit" value="修改"> </td> <?php $this->endWidget();?>
原文地址:http://blog.csdn.net/buyingfei8888/article/details/40222063