Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
428 views
in Technique[技术] by (71.8m points)

gridview - Yii2 grid view search not working ,i cannot understand the error

Category Model

<?php

namespace appmodels;

use Yii;

/**
 * This is the model class for table "category".
 *
 * @property int $id
 * @property string $category_name
 * @property string $water_bill
 * @property string $conservancy
 * @property int|null $siz_width
 * @property int|null $siz_height
 * @property int|null $created_by
 * @property string|null $created_on
 * @property int|null $updated_by
 * @property string|null $updated_on
 */
class Category extends yiidbActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'category';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['category_name',  'conservancy'], 'required'],
            [['siz_width', 'siz_height', 'created_by', 'updated_by'], 'integer'],
            [['created_on', 'updated_on'], 'safe'],
            [['category_name', 'conservancy'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'category_name' => 'Category Name',
            
            'conservancy' => 'Conservancy',
            'siz_width' => 'Size Width',
            'siz_height' => 'Size Height',
            'un.username' => 'Created By',
            'created_on' => 'Created On',
            'name.username' => 'Updated By',
            'updated_on' => 'Updated On',
        ];
    }

                public function getname()
                {
                    return $this->hasOne(User::className(),['id' => 'updated_by']);
                }

                 public function getun()
                {
                    return $this->hasOne(User::className(),['id' => 'created_by']);
                }

}

CategorySearch

#
<?php

namespace appmodels;

use yiiaseModel;
use yiidataActiveDataProvider;
use appmodelsCategory;

/**
 * CategorySearch represents the model behind the search form of `appmodelsCategory`.
 */
class CategorySearch extends Category
{
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id', 'siz_width', 'siz_height', 'created_by', 'updated_by'], 'integer'],
            [['category_name',  'conservancy', 'created_on', 'updated_on'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Category::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'siz_width' => $this->siz_width,
            'siz_height' => $this->siz_height,
            'created_by' => $this->created_by,
            'created_on' => $this->created_on,
            'updated_by' => $this->updated_by,
            'updated_on' => $this->updated_on,
        ]);

        $query->andFilterWhere(['like', 'category_name', $this->category_name])
            
            ->andFilterWhere(['like', 'conservancy', $this->conservancy]);

        return $dataProvider;
    }
}

    

Index.php

<?php

use yiihelpersHtml;
use yiigridGridView;
use yiiwidgetsPjax;
use yiihelpersUrl;

/* @var $this yiiwebView */
/* @var $searchModel appmodelsCategorySearch */
/* @var $dataProvider yiidataActiveDataProvider */

$this->title = Yii::t('app', 'Categories');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="category-index">


    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <div class="card-header py-3 panel" style="">
        <h6 class="m-0 font-weight-bold text-primary"><?= Html::a(Yii::t('app', '+ Add Category'), ['create'], ['class' => 'btn btn-success']) ?>

        </h6>
    </div>
    <div class="card" style="padding:20px; border-radius: 0 0 4px 4px; ">

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'summary' => "",
        'columns' => [
            ['class' => 'yiigridSerialColumn'],

            //'id',

           [
             'attribute'=> 'category_name',
             'value'=> 'category_name',
         ],
         
            'siz_width',
            'siz_height',
            'conservancy',
            //'created_by',
            //'created_on',
            //'updated_by',
            //'updated_on',

            ['class' => 'yiigridActionColumn',
            'header'=>'Action',
            'headerOptions' => ['width' => '80'],
            
            'template' => '{View}{Update}{Delete}',

            'buttons'=> [
                'Delete' => function ($url,$model,$key){
                    return html::a('<i class="fa fa-trash" style="font-size:16px;color:#F05550 "></i>' ,url::to(['/category/delete?id='.$model->id]));
                },
                'View' => function ($url,$model,$key){
                    return html::a('<i class="fa fa-eye" style="font-size:16px;color:#00cc66"></i>' ,url::to(['/category/view?id='.$model->id]));
                },
                'Update' => function ($url,$model,$key){
                    return html::a('<i class="fa fa-edit" style="font-size:16px;color:#4A94D2"></i>',url::to(['/category/update?id='.$model->id]));
                }
            ]
        ],
        ],
    ]); ?>

    </div>
</div>

View.php

[<?php

use yiihelpersHtml;
use yiiwidgetsDetailView;

/* @var $this yiiwebView */
/* @var $model appmodelsCategory */

$this->title = $model->category_name;
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Categories'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
yiiwebYiiAsset::register($this);
?>
<div class="category-view">

    <h1><?= Html::encode($this->title) ?></h1>

    <p>
        <?= Html::a(Yii::t('app', 'Update'), ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
        <?= Html::a(Yii::t('app', 'Delete'), ['delete', 'id' => $model->id], [
            'class' => 'btn btn-danger',
            'data' => [
                'confirm' => Yii::t('app', 'Are you sure you want to delete this item?'),
                'method' => 'post',
            ],
        ]) ?>
    </p>

    <?= DetailView::widget([
        'model' => $model,
        'attributes' => [
          
            'category_name',
            
            
            'siz_width',
            'siz_height',
            'conservancy',
            // 'created_by',
            'un.username',
            'created_on',
            'name.username',
            // 'un.username',
            'updated_on',
        ],
    ]) ?>

</div>]

Controller

<?php

namespace appcontrollers;

use Yii;
use appmodelsCategory;
use appmodelsCategorySearch;
use yiiwebController;
use yiiwebNotFoundHttpException;
use yiifiltersVerbFilter;
use yiifiltersAccessControl;
use appmodelsUser;

/**
 * CategoryController implements the CRUD actions for Category model.
 */
class CategoryController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class'=>AccessControl::classname(),
                'only'=>['create'],
                'rules' =>[
                [

                    'allow'=>true,
                    'roles'=>['@']
                ],
            ]
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Category models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new CategorySearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Category model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Category model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Category();

        if ($model->load(Yii::$app->request->post()) ) {
            $model->created_by = Yii::$app->user->identity->id;
            $model->created_on = date('Y-m-d H:i:s');
            if($model->validate()){
                if($model->save()){
                    return $this->redirect(['view', 'id' => $model->id]);
                }else{

                }
            }
        }
        return $this->render('create', [
            'model' => $model,
        ]);
    }
    /**
     * Updates an existing Category model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post())){
            $model->updated_by = Yii::$app->user->identity->id;
            $model->updated_on = date('Y-m-d H:i:s');
            if($model->validate()){
         if($model->save()) {
             
            return $this->redirect(['view', 'id' => $model->id]);
        }else{

                }
            }
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

    /**
     * Deletes an existing Category model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function a

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...