YII creating Database

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Creating Database

Earlier we performed CRUD operation with yii's Gii generating tool. Now we'll perform CRUD without Gii.

We'll perform a complete CRUD operation here.

The name of our Yii2 folder is dbb.

We have created a database named student and a table inside it named child.

YII Creating database 1

Look at the above snapshot, this is our table's structure.


Database Configuration

To configure your database in Yii2, go to common/config/main-local.php file and write the name of your database.

We have written student as the name of our database.

  1. return [   

  2.     'components' => [   

  3.         'db' => [   

  4.             'class' => 'yiidbConnection',   

  5.             'dsn' => 'mysql:host=localhost;dbname=student',   

  6.             'username' => 'root',   

  7.             'password' => 'mysql',   

  8.             'charset' => 'utf8',   

  9.        ],   

  10.         'mailer' => [   

  11.             'class' => 'yiiswiftmailerMailer',   

  12.             'viewPath' => '@common/mail',   

  13.             // send all mails to a file by default. You have to set   

  14.             // 'useFileTransport' to false and configure a transport   

  15.             // for the mailer to send real emails.   

  16.             'useFileTransport' => true,   

  17.         ],   

  18.     ],   

  19. ];