Yii 2:使用保存的数据表

时间:2017-05-25 17:16:45

标签: mysql yii2 models

我有两个表:站点和路由。站表具有列(station_id,address,office_hours,date_create),Routes表具有列(routes_id,from_id,destination_id)。 Routes表上的from_id和destination_id是引用站表的外键。

现在我想要做的是每当添加一个电台时,电台的路线都是使用表中已有的电台计算的。例如,假设我们已经在站表中有站A.将站B的结果添加到两个路由,A - > B和B - > A,其中路由A - > B,A =>来自B =>目的地,因此只会在路线表上挑选和填充它们的ID。

我在电台控制器中尝试了以下代码,但我没有取得任何成功。代码是:

StationsController:

public function actionCreate()
{
    $model = new Stations();

    $routesModel = new Routes();

    //checking whether we are getting the logged in user id value
    Yii::info("User id=".Yii::$app->user->id); 

    $model->registered_by_id = Yii::$app->user->id;

    $model->status = 10;

    if ($model->load(Yii::$app->request->post()) && $model->save()) {

        //checking here the saved user id value in  table
        Yii::info("checking User id after saving model=".$model->registered_by_id);

        $this->createRoutes($model->station_id);

        return $this->redirect(['view', 'id' => $model->station_id]);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}

public function createRoutes($id)
{
    $model = new Routes;

    $command = Yii::$app->db->createCommand('SELECT station_id FROM stations WHERE station_id !='.$id);
    $all_stations = $command->queryAll();

    foreach ($all_stations as $new_route) {
        $from_id = $id;
        $destination_id = $new_route;
        $model->load(Yii::$app->request->post()) && $model->save();
        $from_id = $new_route;
        $destination_id = $id;
        $model->load(Yii::$app->request->post()) && $model->save();
    }

    return;
}

填充了station表,但未填充routes表。但是,我没有收到任何错误。我哪里可能出错?

请尽可能提供帮助。

修改函数createRoutes()

public function createRoutes($id)
{
    $model = new Routes;

    $count = (new \yii\db\Query())->from('stations')->count();

    if($count > 1) {
        $command = Yii::$app->db->createCommand('SELECT station_id FROM stations WHERE station_id !='.$id);
        $all_stations = $command->queryAll();

        foreach ($all_stations as $new_route) {
            $model->from_id = $id;
            $model->destination_id = $new_route['station_id'];
            $model->save();
            $model->from_id = $new_route['station_id'];
            $model->destination_id = $id;
            $model->save();
        }
        return;
    }
    else
        return;
}

现在,在进行上述更改后,它只保存一条到数据库的路径,但是有几个站,因此应该创建几条路径。如果我有10个站,它将只生成9到10站的路由;我的假设是它只保存了foreach外观的最后一点,而在其他情况下,不会调用$ model-> save()参数。我在这里做错了吗?

3 个答案:

答案 0 :(得分:0)

也许Yii :: $ app-> request-> post()在函数createRoules中不起作用,因为它不是动作函数,但你可以使用$ model-> getErrors( )找到模型中的错误

答案 1 :(得分:0)

我知道这是如此粗糙,在查询时间方面对系统意义重大,但我拼命寻找答案。所以到目前为止这对我有用;

public function actionCreate()
{
    $model = new Stations();

    $routesModel = new Routes();

    //checking whether we are getting the logged in user id value
    Yii::info("User id=".Yii::$app->user->id); 

    $model->registered_by_id = Yii::$app->user->id;

    $model->status = 10;

    if ($model->load(Yii::$app->request->post()) && $model->save()) {

        //checking here the saved user id value in  table
        Yii::info("checking User id after saving model=".$model->registered_by_id);

        $this->createRoutes($model->station_id);

        return $this->redirect(['view', 'id' => $model->station_id]);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}

public function createRoutes($id)
{
    $model = new Routes;

    $count = (new \yii\db\Query())->from('stations')->count();

    if($count > 1) {
        $command = Yii::$app->db->createCommand('SELECT station_id FROM stations WHERE station_id !='.$id);
        $all_stations = $command->queryAll();

        foreach ($all_stations as $new_route) {
            $from_id = $id;
            $destination_id = $new_route['station_id'];
            $sql = 'INSERT INTO routes(from_id, destination_id) VALUES('.$from_id.','.$destination_id.')';
            Yii::$app->db->createCommand($sql)->execute();
            // $model->save();
            $from_id = $new_route['station_id'];
            $destination_id = $id;
            $sql = 'INSERT INTO routes(from_id, destination_id) VALUES('.$from_id.','.$destination_id.')';
            Yii::$app->db->createCommand($sql)->execute();
            // $model->save();
        }
        return;
    }
    else
        return;
}

我仍然欢迎更好的解决方案,特别是使上述效率更高。谢谢你们。

答案 2 :(得分:0)

createRoutes方法中,您只创建一个Routes模型并填充它并将其保存在循环中,这就是为什么您只获得一条记录。相反,你只需要在foreach中创建新模型,如下所示:

public function createRoutes($id)
{
    //$model = new Routes; not here

    // $command = Yii::$app->db->createCommand('SELECT station_id FROM stations WHERE station_id !='.$id);
    // $all_stations = $command->queryAll();

    // I am using  active record instead of single query, you can do anything you want here
    $all_stations = Stations::find()
                ->where('station_id != :id', [':id' => $id])
                ->all();

    foreach ($all_stations as $new_route) {
        $model = new Routes; // create record here
        $model->from_id = $id;
        $model->destination_id = $new_route->station_id;
        // no need to load because these data are not in request param
        // $model->load(Yii::$app->request->post()) && $model->save();
        $model->save();

        $model = new Routes; // and another one
        $model->from_id = $new_route->station_id;
        $model->destination_id = $id;
        $model->save();
    }

    return;
}

重构版本: 上面应该做的伎俩,但这是另一个优化。

Routes模型中

创建一个这样的静态方法:

public static function create($data)
{
    $model = new self;
    $model->from_id = $data['from_id'];
    $model->destination_id = $data['destination_id'];
    return $model->save();
}

然后像这样重写createRoutes方法:

public function createRoutes($id)
{
    $all_stations = Stations::find()
                ->where('station_id != :id', [':id' => $id])
                ->all();

    foreach ($all_stations as $new_route) {
        Routes::create([
            'from_id' => $id,
            'destination_id' => $new_route->station_id,
        ]);

        Routes::create([
            'from_id' => $new_route->station_id,
            'destination_id' => $id,
        ]);
    }

    return;
}

我们现在有更清洁的版本。