Php 将参数传递给yii中的函数

Php 将参数传递给yii中的函数,php,yii,Php,Yii,我是yii的新手。我在CGridView中创建了一个自定义in按钮,它有一个CButtonColumn类。我只是想知道如何传递可以添加到模型中php函数中的参数 这是我在表中的自定义按钮 array( 'class'=>'CButtonColumn', 'template'=>'{approve}, {update},{delete}', 'buttons'=>array( 'approve' =

我是yii的新手。我在CGridView中创建了一个自定义in按钮,它有一个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'];
}

}
公共功能审批者注册($user\u id,$category){
$db=new-PDO('mysql:host=localhost;dbname=secret;charset=utf8','Andy','*****');
$getCounter=“根据注册人说明限额1从柜台订单中选择注册人;”;
$bool=false;
$show='0';
做{
$result=$db->query($getCounter);
//$registents=$db->query($getCounter);
//$result->setFetchMode(PDO::FETCH_ASSOC);
//$注册人='1';
foreach(结果为$value){
$registents=$value['registents'];
回显“你好”。$注册人。
; } //$registents=$result['registents']; //打印(注册人); $max_注册人='3400'; 如果($max_注册人>$注册人){ //将使用$updateCounterByOne的pdo $updateCounterByOne=“更新计数器集注册者=注册者+1,其中注册者=”.$注册者。“;”; $updateCounter=$db->prepare($updateCounterByOne); $updateCounter->execute(); //返回受影响的行 $returnAffectedRows=$updateCounter->rowCount(); $bool=true; //中断; } 否则{ echo“没有更多可用插槽”; //中断; } }而($returnAffectedRows=='0'); 如果($bool=true){ //sql语法 $selectApprovedUser=“从注册人中选择用户id,其中用户id=””。$user\u id。“;”; //将使用$selectApprovedUser的pdo $updateApprovedUser=“更新注册人集已批准='YES'其中user_id=“.$selectApprovedUser。”;”; $updateApproved=$db->prepare($updateApprovedUser); $updateApproved->execute(); //将使用$insertApprovedUser的pdo $insertApprovedUser=“将值(“$user\U id.”、“$category.”、“$curdate()”)插入已批准的注册人(用户id、类别、批准日期)中;”; $insertApproved=$db->prepare($insertApprovedUser); $INSERTAPPORED->execute(); //执行审判 $selectSomething=“从计数器中选择注册人,其中串联='0'; $doSelect=$db->prepare($selectSomething); $doSelect->execute(); $hello=$doSelect->fetchAll(); echo$hello[0][“注册人]; } }
您的问题是在这里完全绕过了控制器

按钮列配置有以下参数

'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
)
有关详细信息,请参阅

正如您所看到的,
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>

你知道一点不复杂的方法吗?我现在没有使用任何javascript。我只是想知道如何通过php传递值。好吧,不使用js/ajax的唯一方法是使用button'url'属性从button url直接向操作发出get请求。注意,这将调用页面进行更改,您可以在那里获得该页面的新视图。我为此修改了按钮参数,这非常有用。谢谢
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();
}