Sami Bekkari
Sami Bekkari

Reputation: 21

Yii Framework failed to insert data to a specific table

After too many research online on how to insert data on a specific table I failed to find any beginning.

I have a table name called yii_tst59 and it has the following fields: - id -idnum -name

I read some tutorial in Yii docs but those are existing tables.

What I want to do is simple , insert data in yii_tst59.

what should I do in /controllers/SiteController.php, in the /models and in /view

I just need to know how to start, please help.

Upvotes: 2

Views: 237

Answers (2)

Nico Savini
Nico Savini

Reputation: 487

The best way to do it, if you want to do it with ActiveRecords is:

  • Generate AR for the new table (you can do it manually or using gii - http://www.yiiframework.com/doc/guide/1.1/en/topics.gii)
  • Use that model to populate the record and save it (see below)

    public function actionTest(){ $testModel = new Yii_tst59(); $testModel->id = 1; ... set other variables... $testModel->save() }

If you do not want to use active records you can insert it directly via SQL but I would not recommend it, specially if you're using Yii for the first times.

Upvotes: 2

Rohit Gaikwad
Rohit Gaikwad

Reputation: 815

you can directly insert your data using following format

$sql = 'Your insert query';
Yii::app()->db->createCommand($sql)->execute();

Upvotes: 0

Related Questions