Yii 2 interview questions

Yii 2 interview questions Download Yii 2 interview questions PDF

Yii 2 interview questions

Yii 2 requires PHP 5.4 or above with mbstring extension and PCRE-support.

Yii 2.0 requires PHP 5.4.0 or above and runs best with the latest version of PHP 7 too.

Yii::app()->request->getUrl() method is used to get current url in Yii framework.

Below are list of some database related functions used in Yii 2 Framework

  • find(): Creates an yii\db\ActiveQueryInterface instance for query purpose.Usages:
    //Getting first record matching with condition
    $user = User::find()->where(['name' => 'Abc'])->one();
    
  • findAll(): Returns a list of active record models that match the specified primary key value(s) or a set of column values.Usages:
    // find the customers whose primary key value is 10
    $customers = Customer::findAll(10);
    // find customers whose age is 30 and whose status is active
    $customers = Customer::findAll(['age' => 30, 'status' => 'active']);
    
  • insert() :Inserts a row into the associated database table using the attribute values of this record.Usages:
    $customer = new Customer;
    $customer->name = $name;
    $customer->email = $email;
    $customer->insert();
    
  • delete() :Deletes the table row corresponding to this active record.Usages:
    $models = Customer::find()->where('status = 3')->all();
    foreach ($models as $model) {
        $model->delete();
    }
  • deleteAll() :Deletes rows in the table using the provided conditions.Usages:
    Customer::deleteAll('status = 3');
  • save() :Saves the current record.Usages:
    $customer = new Customer; // or $customer = Customer::findOne($id);
    $customer->name = $name;
    $customer->email = $email;
    $customer->save();

Read Best Codeigniter interview questions

Yii follows below naming conventions

  • You can define table prefix when using Gii. In your case, you need to set it to tbl_. Then it should generate UserController instead of TblUserController.
  • The Yii Framework employs a class naming convention whereby the names of the classes directly map to the directories in which they are stored. The root level directory of the Yii Framework is the “framework/” directory, under which all classes are stored hierarchically.
  • Class names may only contain alphanumeric characters. Numbers are permitted in class names but are discouraged. Dot (.) is only permitted in place of the path separator.