YII进行数据增删改查分析

关于模型部分参考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";
                    }

                }
	}
?>

通过这个url(http://localhost/shop/index.php?r=houtai/goods/show)show视图:

部分源码:

<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>

add模板部分源码:

           <?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();?>
时间: 2024-08-04 22:17:24

YII进行数据增删改查分析的相关文章

Salesforce零基础(三)简单的数据增删改查页面的构建(Ajax样式)

VisualForce封装了很多的标签用来进行页面设计 下面以一个单一的表进行数据增删改查.表结构如图1所示.通过图可以看出GOODS表自己定义的参数主要包括以下: GoodsName__c,GoodsType__c,GoodsBrand__c,GoodsDescribe__c,GoodsPrice__c. 图1 VF每个页面都是以<apex:page>标签起始</apex:page>结束,每个VF页面都有一个Controller用来控制其业务逻辑.本篇例子中主要用到的控件包括如下

三种方式实现数据增删改查

原生form实现   forms组件实现   modelform组件实现 用原生form实现页面数据增删改查 前端代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>书籍列表</title> </head> <body> <a href="/book/add&qu

Yii数据库操作增删改查-[增加\查询\更新\删除 AR模式]

在Yii的开发中常常需要去使用Yii的增删改查方法,这些方法又可以多次变化和组合,带来全方位的实现对数据库的处理,下面对这些方法做一些简单的整理和梳理,有遗漏或是BUG,敬请指出.灰常感谢!!! 一.查询数据集合 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 //1.该方法是根据一个条件查询一个集合 $admin=Admin::model()->findAll($condition,$params); $admin=Admin::model()->f

函数、文件操作实现数据增删改查---low版本

首先说明这个脚本很low,目前水平有限,只能实现使用固定的语法对数据进行增删改查.但是现在脚本不low,怎么让明年的我来嘲笑今年的自己 需求    a.可进行模糊查询,语法至少支持下面3种:  select name,age from staff_table where age > 22  select  * from staff_table where dept = "IT"      select  * from staff_table where enroll_date l

C#在winform中操作数据库,实现数据增删改查

1.前言: 运行环境:VS2013+SQL2008+Windows10 程序界面预览: 使用的主要控件:dataGridview和menuStrip等. 2.功能具体介绍: 1.首先,我们要先实现基本的数据操作,增删改查这几个操作. (1)先定义一个数据库操作的公共类: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks

jeesite应用实战(数据增删改查)

jeesite配置指南(官方文档有坑,我把坑填了!)这篇文章里,我主要把jeesite官方给出的帮助文档的坑填了,按照里面的方法可以搭建起来jeesite的站点.系统可以运行以后,就可以进入开发模块了,我们先从数据的增删改查做起. 一.页面效果 很简单,涉及到的就是数据的增删改查. 二.如何利用jeesite做呢? 上面我们也看到了,功能很简单,那么怎么利用jeesite做呢?jeesite能给我们提供什么便利呢? 第一步.建表 利用jeesite之前,要先建数据表. 至于怎么建表,方法太多了,

Sql Server2008温故而知新系列02:数据增删改查之&quot;增&quot;

增删改查-数据库最基本使用方法,也是数据库最常用的操作方法: 用到的命令:insert[into] 插入:delete from  删除:update 修改:select 查询. 首先说一说插入的格式(即新增数据): 1.insert into table_name(field1,field2,field3,…………)  values(字段1记录,字段2记录,…………) 如有多行记录重复写入多行 2.insert into table_name(field1,field2,field3,…………

yii添加行的增删改查

效果图: 控制器: <?phpnamespace backend\controllers;use Yii;use yii\web\Controller;use backend\models\Zhan; class IndexController extends Controller{   //显示页面   public function actionIndex()   {        $index=new Zhan();        //接受值         if($_POST)     

SQLite的使用(二):数据增删改查

SQLiteDatabase 用来管理SQLite数据库的类.SQLiteDatabase新增.修改.删除和查询数据库数据的方法,还可以执行其他常见的数据库管理任务. 方法 描述 (void) execSQL(String sql) 执行一个SQL语句的方法 (long) insert(String table,String nullColumnHack,ContentValues values) 添加数据行的方法 (int) update(String table, ContentValues