Part 2 fix problem with model ActiveQuerry

in #dlive6 years ago

Thumbnail

Today i show you how you can find problem with your code.
What you can to do it.
Of this video i show how i lookng for solution for problems with code.
I dont have solution the next video i try to fix it now i can show you good code for your crud system.
This is controller:

<?php

namespace app\controllers;

use Yii;
use app\models\People;
use app\models\PeopleQuery;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**

  • PeopleController implements the CRUD actions for People model.
    /
    class PeopleController extends Controller
    {
    /
    *

    • {@inheritdoc}
      */
      public function behaviors()
      {
      return [
      'verbs' => [
      'class' => VerbFilter::className(),
      'actions' => [
      'delete' => ['POST'],
      ],
      ],
      ];
      }

    /**

    • Lists all People models.
    • @return mixed
      */
      public function actionIndex()
      {
      $searchModel = new PeopleQuery();
      $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
      var_dump($dataProvsearchModelider);
      return $this->render('index', [
      'searchModel' => $searchModel,
      'dataProvider' => $dataProvider,
      ]);
      }

    /**

    • Displays a single People 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 People model.

    • If creation is successful, the browser will be redirected to the 'view' page.

    • @return mixed
      */
      public function actionCreate()
      {
      $model = new People();

      if ($model->load(Yii::$app->request->post()) && $model->save()) {
      return $this->redirect(['view', 'id' => $model->id]);
      }

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

    /**

    • Updates an existing People 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->save()) {
      return $this->redirect(['view', 'id' => $model->id]);
      }

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

    /**

    • Deletes an existing People 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 actionDelete($id)
      {
      $this->findModel($id)->delete();

      return $this->redirect(['index']);
      }

    /**

    • Finds the People model based on its primary key value.

    • If the model is not found, a 404 HTTP exception will be thrown.

    • @param integer $id

    • @return People the loaded model

    • @throws NotFoundHttpException if the model cannot be found
      */
      protected function findModel($id)
      {
      if (($model = People::findOne($id)) !== null) {
      return $model;
      }

      throw new NotFoundHttpException('The requested page does not exist.');
      }
      }

Model:

<?php

namespace app\models;

use Yii;
use yii\db\ActiveQuery;

/**

  • This is the model class for table "{{%people}}".

  • @property int $id

  • @property string $name

  • @property string $surname

  • @property int $pesel
    /
    class People extends ActiveQuery
    {
    /
    *

    • {@inheritdoc}
      */
      public static function tableName()
      {
      return '{{%people}}';
      }

    /**

    • {@inheritdoc}
      */
      public function rules()
      {
      return [
      [['name', 'surname', 'pesel'], 'required'],
      [['pesel'], 'integer'],
      [['name', 'surname'], 'string', 'max' => 150],
      ];
      }

    /**

    • {@inheritdoc}
      */
      public function attributeLabels()
      {
      return [
      'id' => Yii::t('app', 'ID'),
      'name' => Yii::t('app', 'Name'),
      'surname' => Yii::t('app', 'Surname'),
      'pesel' => Yii::t('app', 'Pesel'),
      ];
      }

    /**

    • {@inheritdoc}
    • @return PeopleQuery the active query used by this AR class.
      */
      public static function find()
      {
      return new PeopleQuery(get_called_class());
      }
      }
      Model querry:
<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\People;

/**

  • PeopleQuery represents the model behind the search form of app\models\People.
    /
    class PeopleQuery extends People
    {
    /
    *

    • {@inheritdoc}
      */
      public function rules()
      {
      return [
      [['id', 'pesel'], 'integer'],
      [['name', 'surname'], '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 = People::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,
      'pesel' => $this->pesel,
      ]);

      $query->andFilterWhere(['like', 'name', $this->name])
      ->andFilterWhere(['like', 'surname', $this->surname]);

      return $dataProvider;
      }
      }
      View same index:

<?php

use yii\helpers\Html;
use yii\grid\GridView;

/* @var $this yii\web\View /
/
@var $searchModel app\models\PeopleQuery /
/
@var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Peoples';
$this->params['breadcrumbs'][] = $this->title;
?>

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

<p>
    <?= Html::a('Create People', ['create'], ['class' => 'btn btn-success']) ?>
</p>

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],

        'id',
        'name',
        'surname',
        'pesel',

        ['class' => 'yii\grid\ActionColumn'],
    ],
]); ?>

My video is at DLive

Sort:  

Witaj, jestem botem patrolującym #pl-artykuly.
Twój post nie spełnia podstawowej reguły #pl-artykuly dotyczącej długości wpisu.
Wyedytuj go dopisując więcej treści lub usuń tag - dziękujemy!

Więcej o regulaminie możesz przeczytać w podanym linku.

Coin Marketplace

STEEM 0.20
TRX 0.15
JST 0.030
BTC 65269.02
ETH 2653.11
USDT 1.00
SBD 2.84