Yii2.0 Dynamic Model Creation

As per my previous shared post you have to know what is Yii 2.0 and its awesome features. If you not read these posts please read this and know little bit about Yii2.0 basic(API Web APP) . Today I am sharing you a very new feature of Yii 2.0 which will help us a-lot when we will create REST API in Yii 2.0, You can also learn Yii2.0 REST API creation on my site.

Yii2.0 Dynamic Model creation one of the best part of Yii2.0 . Just think little bit about you need to create every time a model which will effect in your server space little bit, but increase your work in coding part because not only it will create a single file but it will also create some rule and dependencies for db columns which are define in that, so that it will increase complexity in code, also developer also need to take that their is one more fill which might be affect when i change some thing. So in that time Dynamic model class will help us to create a single where we can define many model definition for validation of data which we are posting in db.

Let me show the procedure of Dynamic model creation.

First we should need to create a model class on any name as per follow Yii2.0 Model file creation rule. For this tutorial I am creating Validation.php file in my frontend/Model folder . The code and namespace look like

<?php

namespace frontend\models;

use yii\base\DynamicModel;
use Yii;

/**
 * This is the model class for table "table name".
 *
 * @property integer $id
 */
class Validation extends \yii\db\ActiveRecord {

After create Model Class we should need to define some static function in-side of Validation class. Let me show how

 public static function test($params = array()) {
      $error = array();
      $model = new DynamicModel(compact('id', 'name'));
      $model->addRule(['id', 'name'], 'required');
      $model->defineAttribute('id', @$params['id']);
      $model->defineAttribute('name', @$params['name']);
      $model->validate();
      if ($model->hasErrors()) {
         foreach ($model->getErrors() as $key => $value) {
            $error[$key] = $value[0];
         }
      }
      return $error;
   }

As per above function you can define many more function in your Validation.php file. Now I am defining you what happen in side this function. Just Look and read the test f(n) definition, when you read simply the definition of test function you just see the f(n) has a parameter which is $params which is an array. I build this function for my REST API where I getting many parameters by post way, so I passed $params as array() You can pass any thing. Then I have created a error name array which is for hold errors which will generate in passing value, then I have create a $model object of Dynamic Model class  and inside of that i add two attributes on which i want to add rule in side of function and then i have add required rules on that and after that the procedure same as like other validation rule and then i return the error as return argument of function.

So by the help of same procedure you can create many function as per your requirement in-side of validation.php file. Now let me show how to use this method in controller class. As per Yii2.0 rule you should need to use model class in controller class like this use frontend\models\Validation;

After use class use this Oop’s function class function calling methodology. Like that

$error = Validation::test($params);

So by that way you will create Dynamic model and then you will use this in your controller function. Hope this tut will help you  🙂  Happy coding

Leave a Comment

Your email address will not be published. Required fields are marked *