What's The Meaning Of This Code In Yii


public function actionIndex($id=null)

	{

                $model = new Posts('search');

                $crumbs = array(

                     array('name' => "Home", 'url' => array('site/index')),

                     array('name' => 'Post Search'),

                 );

                $title = "Post Search";

                $criteria = new CDbCriteria();

                $condition="";

                if($id !=null){

                    $model->specialty_id = $id;

                }

                if(isset($_POST['Posts'])){

                    if($_POST['Posts']['specialty_id'] !="-1"){

                        $model->specialty_id = $_POST['Posts']['specialty_id'];

                    }

                    if($_POST['Posts']['location_id'] !="-1"){

                        $model->location_id = $_POST['Posts']['location_id'];

                    }

                    if($_POST['Posts']['grade_id'] !="-1"){

                        $model->grade_id = $_POST['Posts']['grade_id'];

                    }

                    if($_POST['Posts']['pay_amount'] !=""){

                        $model->pay_amount = $_POST['Posts']['pay_amount'];

                    }

                }

can anyone explain me this code.I cant understand it.what’s the meaning of !=-1.pls explain this code.

thanx in advance. ::)

Well, it means the same as in PHP: If the value in the submitted form doesn’t match “-1”, don’t assign the following value. Not sure why it compares to the string “-1” rather than the numeric value -1 …

I suppose somebody has created dropdown lists (speciality, location, grade) and marked values non-selectable options (for example, option with text "please select your speciality") as -1.

That’s not very good and clear, since Yii has special option for that cases.

Looks like that he need to let some values null, but you can mark them as nullable in validators.


array('nullableAttribute', 'default', 'setOnEmpty'=>true, 'value'=>null ),			



You can let the ddl to return empty for an empty value, and usung this special validator you will get null in the database, so your controller code can be changed to:


public function actionIndex($id=null)

        {

                $model = new Posts('search');

                $crumbs = array(

                     array('name' => "Home", 'url' => array('site/index')),

                     array('name' => 'Post Search'),

                 );

                $title = "Post Search";

                $criteria = new CDbCriteria();

                $condition="";

                if($id !=null){

                    $model->specialty_id = $id;

                }

                if(isset($_POST['Posts'])){

                        $model->attributes = $_POST['Posts'];

                    }

                }

thanx lot guys.

and pls tell me

what the purpose of this code

$model = new Posts(‘search’);

Hi,

It will trigger search funtionality in post controller.

Thanks,

chandran nepolean

Thanx lot