Yii 2.0: Create Crud with Yii2

10 followers

This guide is meant to help you started with Yii2. Yii2 is by no means "production" ready. I do not recommend using this in production. lets start with yii2:

I assume you already know how to install Yii2, check (Yii2 basic installation).

1- Open /config/web.php to configure DB connection and other configuration. add DB connection as below :

'components' => array(
  'db' => array(
      'class' => 'yii\db\Connection',
        'dsn' => 'mysql:host=127.0.0.1;dbname=deploy', // MySQL, MariaDB
      //'dsn' => 'sqlite:/path/to/database/file', // SQLite
      //'dsn' => 'pgsql:host=localhost;port=5432;dbname=mydatabase',
      // PostgreSQL
      //'dsn' => 'cubrid:dbname=demodb;host=localhost;port=33000',
      // CUBRID
      //'dsn' => 'sqlsrv:Server=localhost;Database=mydatabase',
      // MS SQL Server, sqlsrv driver
      //'dsn' => 'dblib:host=localhost;dbname=mydatabase', 
      // MS SQL Server, dblib driver
      //'dsn' => 'mssql:host=localhost;dbname=mydatabase',
      // MS SQL Server, mssql driver
      //'dsn' => 'oci:dbname=//localhost:1521/mydatabase', // Oracle
            'username' => 'admin',
            'password' => '@dmin123',
            'charset' => 'utf8',
                    ),

Change the DB name whatever you want.

2- Open /config/console.php to add the same for DB connection:

'components' => array(
  'db' => array(
      'class' => 'yii\db\Connection',
        'dsn' => 'mysql:host=127.0.0.1;dbname=deploy', // MySQL, MariaDB
      //'dsn' => 'sqlite:/path/to/database/file', // SQLite
      //'dsn' => 'pgsql:host=localhost;port=5432;dbname=mydatabase',
      // PostgreSQL
      //'dsn' => 'cubrid:dbname=demodb;host=localhost;port=33000',
      // CUBRID
      //'dsn' => 'sqlsrv:Server=localhost;Database=mydatabase',
      // MS SQL Server, sqlsrv driver
      //'dsn' => 'dblib:host=localhost;dbname=mydatabase', 
      // MS SQL Server, dblib driver
      //'dsn' => 'mssql:host=localhost;dbname=mydatabase',
      // MS SQL Server, mssql driver
      //'dsn' => 'oci:dbname=//localhost:1521/mydatabase', // Oracle
            'username' => 'admin',
            'password' => '@dmin123',
            'charset' => 'utf8',
                    ),

3- Open http://localhost//web/index.php?r=gii , for creating models, CRUD, controllers , etc . .

4- First we create models based on our DB schema (we used deploy schema):

  • Put the , Table Name, which you have write manually. e.g(servers as table name).
  • In the model name put {Servers}, and click preview button.
  • Click generate to create {models/Servers.php}.

5- Create CRUD for all models we created recently :

  • In the model class filed put {app\models\Servers}, change Servers to your, own model name.
  • In 'Search model class', you could put {app\models\ServersSearch}, or create folder under models {search} to include all search models, for your models. e.g {app\models\Search\Servers}
  • In 'Controller Class', put {app\controllers\ServersController}. change other option based on your requirements. Finally Click on Generate.

Now CRUD is ready to customize .

Thanks

Total 2 comments

#15027 report it
KonApaz at 2013/09/30 05:14am
wiki without body?

Please somebody moderator or administrator inform the Author of this wiki to includes the content

#15023 report it
prchakal at 2013/09/30 01:49am
Where is the response?

Where is the response?

Leave a comment

Please to leave your comment.

Write new article