2

Hy guys, How to create table in PDO yii2? this code in my controller

$db = new yii\db\Connection([
        'dsn' => 'pgsql:host=localhost;dbname=mydata',
        'username' => 'local',
        'password' => 'bukapeta',
        'charset' => 'utf8',
    ]);


 $commandR = "CREATE TABLE mencoba(nama_tempat TEXT ,tanggal TEXT ,waktu TEXT ,alamat TEXT ,jenis_tempat TEXT ,keterangan TEXT ,foto_lokasi TEXT ,latitude TEXT ,longitude TEXT ,tanggal_buat TEXT ,update_terakhir TEXT ,pembuat TEXT);"
 $command = $db->createCommand($commandR);
 $command->execute();

enter image description here

but this error please help me..

SO-user
  • 1,458
  • 2
  • 21
  • 43
  • Welcome to stackoverflow! Your problem is right there in the error message: `Class 'Yii\db\Connection' not found`. You should try replacing those backslashes ("\") with forward slashes ("/")... – bassim Feb 02 '15 at 12:56
  • try: `new \yii\db\Connection` -> see the leading slash – deacs Feb 02 '15 at 12:59

2 Answers2

4

add $db->open()

$db = new \yii\db\Connection([
    'dsn' => 'pgsql:host=localhost;dbname=mydata',
    'username' => 'local',
    'password' => 'bukapeta',
    'charset' => 'utf8',
]);
$db->open();
Kinjal Dixit
  • 7,777
  • 2
  • 59
  • 68
Hiren Bhut
  • 1,166
  • 1
  • 13
  • 19
1

You can use the method createTable of the queryBuilder. For example :

$queryBuilder = rows = (new \yii\db\Query());
$sql = $queryBuilder->createTable('myTable', [
    'id' => 'pk',
    'myColumn' => 'string',
    'myOtherColumn' => 'number'
]);
n1c01a5
  • 88
  • 9