문제

I am a bit new to the Yii Framework. I am making a product selling website, which has 3 basic models
1. Users model containing the primary key id
2. Products model containing the primary key id
3. Orders model which is basically a mapping between the products and orders. It contains the fields product_id and user_id as foreign keys.

I have made a page where all the products are populated and the logged in user can click on a button on product box to order a particular product.
the code of the link is like this

<?php echo CHtml::link('Order Now',array('order', 'product_id'=>$model->id, 'user_id'=>Yii::app()->user->id)); ?>
(Q1) This is sending a GET request but I want to sent the details as post request. How to do this?

My default controller is the site controller. I have made an actionOrder method in this controller. The code is:

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

But this code is showing bunch of errors. Also, (Q2) how can I put both products_id and users_id in a single array Orders so that I just have to write $_POST['orders'] Also, (Q3) how can I display a flash message after the save is successful?

Kindly help me to solve my 3 problems and sorry if you feel that the questions are too stupid.

도움이 되었습니까?

해결책

Q1: If you want to use POST request, you're going to have to use a form of sorts, in this case the CActiveForm.

Controller:

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
    }
}

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(); ?>

Please note that I have changed the name of the product model variable $model to $product, because we will be using $model for the Orders model for the form.

Q2: In this case I set the users_id value in the controller, so $_POST['Orders'] only contains the value for products_id. In yii you can also mass assign your attributes with:

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

Which basicly means $_POST['Orders'] is already an associative array containing the attribute names and values that are in your form.

Q3: The code shows you how to set and show a flash message after an order is succesfull.

다른 팁

First you have to declare forms send method, if you're using bootsrap it'll be like mine:

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

Second if you want to send custom inputs, you have to specify, otherwise it'll be like

i'll be back to finish this

For your questions 1 and 2 I'd recomend you to use a CActiveForm class. For example

<?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(); ?>

where $model is instance of Orders class, passed by variables thru controller, or set in view file. After that you can use it in way you wanted $model->attributes = $_POST['orders'] in your action method.

For flash message you can use Yii->app()->user->setFlash('orderStatus', 'Successful'), before redirect( or render ) in your actionOrder. To show it:

<?php if(Yii::app()->user->hasFlash('orderStatus')):?>
<div class="info">
    <?php echo Yii::app()->user->getFlash('orderStatus'); ?>
</div>
<?php endif; ?>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top