How To Send The Request Through Post And How To Access Attributes Of The Model?

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

b[/b] 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, b[/b] 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, b[/b] 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.

  1. You’d have to create a button in a form tag that would submit two hidden fields to your order action.

  2. Use the following argument to any method building an url - Controller.createUrl or CHtml.link:




array('order', 'Orders'=>array('product_id'=>$model->id, 'user_id'=>Yii::app()->user->id))



Your url params instead of this:




product_id=X&user_id=Y



will look like this:




Orders[product_id]=X&Orders[user_id]=Y



If you use ActiveForm and/or CHtml.active* methods to draw form fields they will be named like that automatically using your model’s and attribute names.

  1. After successfully saving the model, before the redirect, call the setFlash() method:



Yii::app()->user->setFlash('success', 'Operation completed successfully.');



Then in the action you’ve redirected the user to, in the view, render all flashes:




foreach(Yii::app()->user->getFlashes() as $flash) {

    // display them as you prefer to

}



I invite you to read the guide if you haven’t already and use the API docs on a regular basis.