如何通过 POST 发送请求以及如何访问模型的属性


How to send the request through POST and how to access attributes of the model?

我对Yii框架有点陌生。我正在制作一个产品销售网站,该网站有 3 个基本模型
1. 包含主键 ID
的用户模型 2. 包含主键 ID
的产品型号 3.订单模型,基本上是产品和订单之间的映射。它包含字段product_iduser_id作为外键

我制作了一个页面,其中填充了所有产品,登录用户可以单击产品框上的按钮来订购特定产品。
链接的代码是这样的

<?php echo CHtml::link('Order Now',array('order', 'product_id'=>$model->id, 'user_id'=>Yii::app()->user->id)); ?>
(Q1)这是发送GET请求,但我想将详细信息作为post请求发送。怎么做?

我的默认控制器是站点控制器。我在这个控制器中做了一个动作订单方法。代码为:

if(Yii::app()->user->isGuest){
$this->redirect('login');
}else{
    $model=new Orders;
    if(isset($_POST['products_id']))
    {
        $model->attributes->products_id=$_POST['product_id'];
        $model->attributes->users_id=Yii::app()->user->id;
        if($model->save())
            $this->redirect(array('index'));
    }
    $this->render('index');
}

但是这段代码显示了一堆错误。另外,(Q2)如何将products_idusers_id放在一个数组中Orders这样我只需要编写$_POST['orders'] 另外,(Q3)保存成功后如何显示闪存消息?

帮我解决我的 3 个问题,如果您觉得这些问题太愚蠢,请道歉。

Q1:如果你想使用POST请求,你必须使用一种形式,在本例中是CActiveForm。

控制器:

public function actionOrder() 
{
    if(Yii::app()->user->isGuest)
        $this->redirect('login');
    else 
    {
        $model=new Orders;
        if(isset($_POST['Orders']))
        {
            $model->product_id=$_POST['Orders']['products_id'];
            $model->users_id = Yii::app()->user->id;
            if($model->save()) 
            {
                // Q3: set the flashmessage
                Yii::app()->user->setFlash('ordered','The product has been ordered!'); 
                $this->redirect(array('index'));
            }
        }
        $this->render('index', array('model'=>$model)); //send the orders model to the view
    }
}

视图:

<!-- Q3: show the flash message if it's set -->
<?php if (Yii::app()->user->hasFlash('ordered')): ?>
    <?php echo Yii::app()->user->getFlash('ordered');  ?>
<?php endif ?>
...
<?php $form=$this->beginWidget('CActiveForm', array('id'=>'order-form')); ?>
<?php echo $form->hiddenField($model,'products_id',array('value'=>$product->id)); ?> // please note the change of variable name
<?php echo CHtml::submitButton('Order Now'); ?>
<?php $this->endWidget(); ?>

请注意,我已将产品模型变量的名称$model更改为$product,因为我们将使用$model作为窗体的订单模型。

Q2:在这种情况下,我在控制器中设置了users_id值,因此$_POST['Orders']只包含products_id的值。在 yii 中,您还可以批量分配您的属性:

$model->attributes = $_POST['Orders']

这基本上意味着$_POST['Orders']已经是一个关联数组,其中包含表单中的属性名称和值。

Q3:该代码向您展示如何在订单成功后设置和显示闪光消息。

首先,您必须声明表单发送方法,如果您使用的是bootsrap,它将像我的一样:

<?php $form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
    'action' => Yii::app()->createUrl($this->route),
    'method' => 'post',
    'id' => 'activity_timeRpt',
));
?>

其次,如果要发送自定义输入,则必须指定,否则会像

我会回来完成这个

对于您的问题 1 和 2,我建议您使用 CActiveForm 类。例如

<?php $form = $this->beginWidget('CActiveForm', array(
    'action' => 'you_action_here'
    'method'=>'post' // this is optinal parameter, as 'post' is default value
)); ?>
<?php echo $form->textField($model,'product_id'); ?>
<?php echo $form->hiddenField($model,'user_id', array('value'=>Yii::app()->user->id)); ?>
<?php $this->endWidget(); ?>

其中$model是类Orders实例,由变量通过控制器传递,或在视图文件中设置。之后,您可以按照您想要的方式使用它$model->attributes = $_POST['orders']在您的操作方法中。

对于 Flash 消息,您可以在 actionOrder 中的重定向(或渲染)之前使用 Yii->app()->user->setFlash('orderStatus', 'Successful') 。要显示它:

<?php if(Yii::app()->user->hasFlash('orderStatus')):?>
<div class="info">
    <?php echo Yii::app()->user->getFlash('orderStatus'); ?>
</div>
<?php endif; ?>