How to create table in yii with use createcommand or other?

how to create a table using createCommand in Yii? I've followed the instructions in the http://www.yiiframework.com/doc/api/1.1/CDbCommand but I am still confused. Can you give an example of creating a table using createCommand in yii. thank you

Upvotes: 0

Views: 2093

Answers (2)

I use this code and sucess..

Yii::app()->db->createCommand("CREATE TABLE {$nama_data}(id serial, {$list_field}, x text,y text,wkt text, the_geom geometry,PRIMARY KEY(id));")->query();

Upvotes: 0

Jerome
Jerome

Reputation: 291

Assuming you've named your database connection string db (in protected/config/main.php) as follows,

'components'=>array(
    'db'=>array(
    ),
),

you should be able to create a table using the following command:

$sqlQuery = "CREATE TABLE IF NOT EXISTS pet (name VARCHAR(20), owner VARCHAR(20))";
$sqlCommand = Yii::app()->db->createCommand($sqlQuery);
$sqlCommand->execute();

You should also be able to replace the first line with any SQL statement and execute it successfully. If you want to query for data, you will need to use queryRow, queryColumn, or queryScalar (as defined in the documentation).

Hope this helps!

Upvotes: 1

Related Questions