单击按钮时,将从主表值插入其他表

时间:2018-08-02 03:10:02

标签: php yii

所以我想在单击按钮时在表中插入一个值,但是要插入的值是从控制器中的其他函数中提取的,我该怎么做?

 public function actionInsert(){
    $description = $_POST['description'] ;
    $amount      = $_POST['amount'] ;
    $project     = $_POST['project'];

    $query = "INSERT INTO pengajuan VALUES ('test','1','projectA')";
    $db->createCommand($query)-execute();
 }
 public function actionSecondInsert{

 //so i want this function to do the same as previous function but insert 
 into different tables with the same value as the previous
 }

2 个答案:

答案 0 :(得分:0)

我的解决方案是将参数放入actionInsert函数中,而不是使两个函数具有几乎完全相同的代码。

 public function actionInsert($table){
    $description = $_POST['description'] ;
    $amount      = $_POST['amount'] ;
    $project     = $_POST['project'];

    $query = "INSERT INTO ".$table." VALUES ('test','1','projectA')";
    $db->createCommand($query)-execute();
 }

答案 1 :(得分:0)

您可以编写在两个动作中调用的函数

function insert($db, $table) {
     $db->createCommand()
    ->insert(
        $table,
        array(
            'column1'=>'test', // here you can write your columns
            'column2'=>1,
            'column3'=>'ProjectA'
        )
    );
}

然后在通话中您可以像$this->insert($db, $table);

一样拨打电话