将参数传递给yii中的函数

时间:2014-06-17 06:02:28

标签: php yii

我是yii的新手。我在CGridView中创建了一个自定义按钮,它有一个CButtonColumn类。我只是想知道如何传递我可以添加到模型中的php函数的参数。

这是表格中的自定义按钮

    array(
        'class'=>'CButtonColumn',
        'template'=>'{approve}, {update},{delete}',
        'buttons'=>array(

            'approve' => array(
                'label'=>'Approve',
                'options'=>array(),
                'click'=>$model->approveRegistrants("$user_id, $category", array("id"=>$data->user_id , "category"=>$data->category),
                )
           )
     )

这是我的功能

public function approveRegistrants($user_id, $category){

$db = new PDO('mysql:host=localhost; dbname=secret; charset=utf8', 'Andy', '*****');
$getCounter = "SELECT registrants FROM counter order by registrants desc limit 1;";
$bool = false;
$show = '0';


do{
    $result = $db->query($getCounter);
    // $registrants = $db->query($getCounter);
    // $result->setFetchMode(PDO::FETCH_ASSOC);
    // $registrants = '1'; 

foreach ($result as $value){
    $registrants = $value['registrants'];
    echo 'hello'.$registrants.'</br>';
}

    // $registrants = $result['registrants'];
    // print_r($registrants);
    $max_registrants = '3400';
        if($max_registrants > $registrants){

        // pdo that will use $updateCounterByOne
        $updateCounterByOne = "UPDATE counter set registrants = registrants + 1 WHERE registrants = ". $registrants .";";
        $updateCounter = $db->prepare($updateCounterByOne);
        $updateCounter->execute();

        // return affected rows
        $returnAffectedRows = $updateCounter->rowCount();
        $bool = true;
        // break;
        }
        else{
        echo "No more slot Available";
        // break;
        }
}while($returnAffectedRows == '0');


if($bool = true){
    //sql syntax
    $selectApprovedUser = "SELECT user_id FROM registrants WHERE user_id = '". $user_id ."';";

    //pdo that will use $selectApprovedUser
    $updateApprovedUser = "UPDATE registrants set approved = 'YES' where user_id = ". $selectApprovedUser .";";
    $updateApproved = $db->prepare($updateApprovedUser);
    $updateApproved->execute();

    //pdo that will use $insertApprovedUser
    $insertApprovedUser = "INSERT INTO approved_registrants (user_id, category, approved_date) VALUES ('".$user_id."', '".$category."', 'curdate()');";
    $insertApproved = $db->prepare($insertApprovedUser);
    $insertApproved->execute();

    //execute trial
    $selectSomething = "SELECT registrants from counter where tandem = '0'";
    $doSelect = $db->prepare($selectSomething);
    $doSelect->execute();
    $hello = $doSelect->fetchAll();
    echo $hello[0]['registrants'];
}

}

1 个答案:

答案 0 :(得分:1)

你的问题是你完全绕过了控制器。

按钮列配置了以下参数

'buttonID' => array(
    'label'=>'...',     // text label of the button
    'url'=>'...',       // a PHP expression for generating the URL of the button
    'imageUrl'=>'...',  // image URL of the button. If not set or false, a text link is used
    'options'=>array(...), // HTML options for the button tag
    'click'=>'...',     // a JS function to be invoked when the button is clicked
    'visible'=>'...',   // a PHP expression for determining whether the button is visible
)

有关详细信息,请参阅CButtonColumn

正如您所看到的,click必须是一个js函数,在单击按钮时将调用该函数。您可以像这样重写按钮

array(
        'class'=>'CButtonColumn',
        'template'=>'{approve}, {update},{delete}',
        'buttons'=>array(
            'approve' => array(
                'label'=>'Approve',
                'options'=>array(),
                // Alternative -1 Url Method -> will cause page to change to approve/id
                 'url'=>'array("approve","id"=>$data->id)',  
                //  Alternative -2 Js method -> use 1,2 not both
                'click'=>'js:approve()',
                )
           )
     )

在您添加的CGridView配置中

array( 
      ....
     'id'=>'gridViewID', //Unique ID for grid view
     'rowHtmlOptionsExpression'=> 'array("id"=>$data->id)', 
)

这样每行都有唯一的ID,(你可以对一个按钮做同样的事情,但由于$ data不可用,因此稍微困难一些)

在你的js函数中你可以做到这一点。

<script type="text/javascript">

    function approve(){
        id = $(this).parent().parent().attr("id");
        <?php echo CHtml::ajax(array( // You also can directly write your ajax 
            'url'=>array('approve'),
            'type'=>'GET',
            'dataType'=>'json',
            'data'=>array('id'=>'js:id'),
            'success'=>'js:function(json){
                $.fn.yiiGridView.update("gridViewID",{}); 
                            // this will refresh the view, you do some other logic here like a confirmation box etc

            }'
        ));?>
    }
</script>

最后你的批准行动

class YourController extend CController {

......

public function actionApprove(){
   id = Yii::app()->request->getQuery('id');
   $dataModel = MyModel::model()->findByPk($id); // This is the model has the $user_id, and $category
   ....
   $OtherModel->approve($dataModel->user_id,$dataModel->category) // if approve is in the same model you can self reference the values of category and user_id directly no need to pass as parameters.
   // Do some logic based on returned value of $otherModel->approve() 
   // return the values from the approve() function and echo from here if required back to the view, directly echoing output makes it difficult to debug which function and where values are coming from .
   Yii::app()->end();
}
相关问题