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)ショービュー:
ソースの一部:
<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();?>
なぜGoodsかについて:model()とnew Goods()はsaveを実行して、それぞれudateとinsertの方法を呼び出します。CActive Recordのソースコードを見ることができます。