Shawn Sonnier
Shawn Sonnier

Reputation: 533

Inserting records into a database in Yii2

Ok, Im trying to insert some records in the database by creating a command and then using insert like in the documentation I found, but I get error "class app\commands Yii" not found. Now I have this at the top of my controller

namespace app\commands;

use yii\console\Controller;

And this is the command i built

//added game to database
    foreach ($games as $g) 
    {
        //echo $g[0].'<br>'.$g[1].'<br>'.$g[2].'<br>'.$g[3].'<br>'.$g[4].'<br>'.$g[5].'<br><br>';
        //take the upper and put into database
        $command = Yii::app()->db->createCommand();

        $command->insert('nfl_lines',array('away'=>$g[0],'home'=>$g[1]));

    }

Upvotes: 2

Views: 2061

Answers (1)

Thao Ngo
Thao Ngo

Reputation: 3771

namespace app\commands;
use Yii;
use yii\console\Controller;

And

Yii::$app->db->createCommand();

You are using yii2, something different from yii1

Upvotes: 2

Related Questions