【问题标题】:Passing parameters to a function in yiiyii中给函数传参数
【发布时间】:2014-06-17 18:39:50
【问题描述】:

我是 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'];
}

}

【问题讨论】:

    标签: php yii


    【解决方案1】:

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

    buttons列配置如下参数

    '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();
    }
    

    【讨论】:

    • 你知道一点不复杂的方法吗?到目前为止,我没有使用任何 javascript。我只是想知道如何通过 php 传递值。
    • 嗯,没有 js/ajax 的唯一方法是使用按钮 'url' 属性直接从按钮 url 向操作发出 get 请求。请注意,这将调用页面进行更改,您可以在那里获得该页面的全新视图。我已经为此修改了按钮参数
    • 这很有帮助。谢谢
    猜你喜欢
    • 1970-01-01
    • 2013-01-31
    • 2013-01-27
    • 2021-04-18
    • 2015-07-05
    • 1970-01-01
    相关资源
    最近更新 更多