Showing posts with label yii2model. Show all posts
Showing posts with label yii2model. Show all posts

Thursday, August 6, 2015


save() OR insert()

$model = new User;
$model->name = 'YII';
$model->email = 'yii2@framework.com';
$model->save();  // equivalent to $model->insert();

insert() command

$connection->createCommand()
 ->insert('tbl_user', [
   'name' => 'yii',
   'status' => 1,])
 ->execute();

batchInsert()

$connection->createCommand()
  ->batchInsert('tbl_user', ['name', 'status'], 
  [
   ['Bala', 1],
   ['Akilan', 0],
   ['Babu', 1],
  ])
  ->execute();


Tất cả hướng dẫn đều được sử dụng trên Yii 2.0 bản Basic

- Vào basic/config/db.php: (basic là tên thư mục gốc)

<?php
return [
 'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=localhost;dbname=reportsa_macromoney', // 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' => 'root',
    'password' => '$123',
    'charset' => 'utf8',
 ];
?>  

- Tiếp theo vào basic/web/config/web.php:

'components' => [    
  'db'=>require(__DIR__ . '/db.php'),
  .................
 ]