yii2.0 learning journey (b)

Introduction: The last time we simply know a bit yii2.0 installation, basic model (add, delete, change, search) operations

 

First, the front and back data exchange

* If you think the default top style ugly, it can be turned off *

 

* Bottom may be so turned off *

 

(1) mvc cooperative operation data

A controller (c), a display method and a receiving method

    /**
     * 列表
     * @return string
     */
    public function actionIndex(){
        $where = array();
        $is_page = false;
        FcArticle::getConditionByList('a_id,article_title,author',$where,'a_id desc',$is_page,10);
        return $this->render('index', [
            'list' => FcArticle::$countries,
            'pagination' => FcArticle::$pagination,
            'is_page'=>$is_page
        ]); 
    } 

    / * * 
     * Add 
     * @return String 
     * / 
    public  function actionAdd () 
    { 

        $ Model = new new FcArticle ();
         // if authenticated, receives data 
        IF ( $ Model -> Load (\ Yii :: $ App -> request-> POST ()) && $ model -> the validate ()) 
        { 
            // the model editing the rules which, after passing through the operational data storage 
            $ model -> INSERT ();
             return  $ the this -> the redirect ( 'the index.php = R & lt Test / index?' ); 
        } 
        the else 
        {
            return  $ the this -> the render ( 'the Add', [
                 'Model' => $ Model , 
            ]); 
        } 
    } 

    / * * 
     * Edit 
     * @return String 
     * / 
    public  function actionUpdate () 
    { 
        // if validated, receive data 
        IF (\ Yii :: $ App -> request-> isGet) {
             $ A_ID = \ Yii :: $ App -> request-> GET ( 'A_ID' );
             // Yii :: $ APP-> request-> queryParams ; // get the request mode, multi-dimensional array 
        } the else  IF (\ Yii :: $ App-> request-> isPost) {
             $ postParams = \ Yii :: $ App -> request-> bodyParams; // POST request method, a multidimensional array 
            $ A_ID = $ postParams [ 'FcArticle'] [ 'A_ID' ]; 
        } 

        $ Model = FcArticle the findOne :: ( Array ( 'A_ID' => $ A_ID ));
         IF ( $ Model -> Load (\ Yii :: $ App -> request-> POST ()) && $ Model -> the validate () ) 
        { 
            // edited rules model which, by then, the operation data storage 
            // https://www.jianshu.com/p/4d5a3a8256d3 include the URL, here is how to remove the index.php approach 
            $ Model ->update();
            return $this->redirect('index.php?r=test/index');
        }
        else
        {
            return $this->render('update', [
                'model' => $model,
                'a_id'=>$a_id
            ]);
        }
    }

Models (m), himself a bit in the original package basis

To reference a page framework document

use Yii \ Data \ Pagination;     // paging class
    / * * 
     * According to criteria query multiple data 
     * @param string $ field field 
     * @param array $ condition condition 
     * @param string $ order sort 
     * @param bool $ page if there are tabs 
     * @param int $ pagesize pages 
     * / 
    public  static  function getConditionByList ( $ Field = '', $ for condition Condition = Array (), $ Order = 'desc A_ID', $ Page = to false , $ pageSize = 10 ) 
    { 
        $ Query = Self :: Find ();
         IF ( $ Page ) { 
            Self:: $ the pagination = new new Pagination ([
                 'defaultPageSize' => $ pageSize , // page shows the number of 
                'The totalCount' => Self :: getConditionCount ( $ for condition Condition ), // total number of 
            ]); // tab pass reference 

            IF ( isset ( $ Field ) && $ Field = ''! ) {
                 $ Query -> SELECT ( $ Field ); 
            } 

            Self :: $ Countries = $ Query 
                -> WHERE ( $ for condition Condition)
                 -> orderBy ( $ the Order )
                 -> offset (Self :: $ pagination -> offset) // offset 
                -> limit (Self :: $ pagination -> limit)
                 -> All (); // queried data tab 
        } the else { 

            IF ( isset ( $ Field ) && $ Field = ''! ) {
                 $ Query -> SELECT ( $ Field ); 
            } 

            Self :: $ Countries = $ Query 
                -> WHERE (for condition Condition $ )
                 -> orderBy ( $ the Order )
                 -> All (); // data to query paging 
        } 
    } 

    / * * 
     * Get the total depending on conditions 
     * @param Array $ for condition Condition 
     * @return int | String 
     * / 
    public  static  function getConditionCount ( $ for condition condition = Array ()) {
         return Self :: Find () -> WHERE ( $ for condition condition ) -> COUNT (); 
    } 

    / * * 
     * data according to the single query condition 
     * @param String Field $  
     * @ param array $ condition
     * @ return FcArticle | array | null
     */
    public function getOneConditionInfo($field = '',$condition = array()){
        $query = self::find();
        if(isset($field) && $field!=''){
            $query->select($field);
        }
        return $query->where($condition)->one();
    }

View (v) rendering, is used here to form component built yii2.0

index.php (list)

<?php
use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\LinkPager;  //引用分页link
?>
<style>
    .list{
        width: 100%;
    }
    .list .l-l,.l-l-h{
        width:100%;
        text-align: center;
    }
    .l-l-h{
        font-weight: bold;
    }
    .list .l-v,.l-h{
        border: 1px solid #0b72b8;
        display:inline-block;
        width: 150px;
        float:left;
        padding: 0px;
        margin: 0px;
        text-align: center;
    }
    .l-h{
        font-weight: bold;
    }
    .clear{ clear:both}
</style>
<?= Html::a('添加', ['test/add'], ['class' => 'profile-link']) ?>
<div class="list">
    <div class="l-l-h">
        <p class="l-h">
            标题
        </p>
        <p class="l-h">
            作者
        </p>
        <p class="l-h">
            操作
        </p>
        <div class="clear"></div>
    </div>
    <?php foreach ($list as $key=>$value){?>
        <div class="l-l">
            <p class="l-v">
                <?= $value['article_title']?>
            </p>
            <p class="l-v">
                <? = $ Value [ 'author']?> 
            </ P> 
            <P class = "LV"> 
                <? :: A = the Html ( 'edit', [ 'Test / Update', 'A_ID' => $ value [ 'A_ID']], [ 'class' => 'Profile-Link'])?> 
            </ P> 
            <div class = "Clear"> </ div> 
        </ div> 
    <? PHP}?> 
        <? PHP
         iF ( $ is_page ) { // if there is paging, there is displayed, then no closed 
        ?> 
        <? :: = LinkPager the widget ([
             'pagination'=>$pagination,
            // 'Options' => [ 'class' => 'hidden'] // tab comes off
            'firstPageLabel' => "Home", 
            'prevPageLabel' => 'Back', 
            'nextPageLabel' => 'Next', 
            'lastPageLabel' => 'Last', 
        ])
          ?> 
        <? PHP}? > 
</ div>

add.php (increase)

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
$form = ActiveForm::begin(['action' => ['test/add'],'method'=>'post']);
?>
<?= $form->field($model, 'article_title')->label('标题名') ?>
<?= $form->field($model, 'author')->label('作者') ?>
<?= Html::submitButton('提交', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>
<?php
ActiveForm::end()
?>

update.php (edit)

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
$form = ActiveForm::begin(['action' => ['test/update'],'method'=>'post']);
?>
<?= $form->field($model, 'article_title')->label('标题名') ?>
<?= $form->field($model, 'author')->label('作者') ?>
<?= $form->field($model, 'a_id')->hiddenInput(['value'=>$a_id]) ?>
<?= Html::submitButton('提交', ['class'=>'btn btn-primary','name' =>'submit-button']) ?>
<?php
ActiveForm::end()
?>

yii2.0 Notes

1, the controller method name must be a small hump way;

For example: actionFormadd, error: actionFormAdd (This visit is less than)

2, was going to be a native form tag, however, found not compatible with validation rules, so do strike

 

Related Links

yii2 form validation Method: https://blog.csdn.net/song_csdn1550/article/details/51004815

yii2.0 Activeform form part components use: https://www.cnblogs.com/ymk0375/p/6285217.html

The method of the view controller yii2.0 Form Form Form at: https://www.cnblogs.com/jiufen/p/5086162.html

Guess you like

Origin www.cnblogs.com/FLy-1992/p/11672751.html