0

I am getting the error undefined variable model. I have the following codes in my view and controller. My aim is to search the results and show it in same page. I have tried the following things but its not working.

Again,I this this the conventional yii method to do so,or do I have to use a search() function from model SearchEmployee().?

public function actionSearch()
{
      $model = new SearchEmployee();

       /*Getting Data From Search Form For Processing */
  if (isset($_POST['SearchEmployee'])) {

      $category   =  $_POST['SearchEmployee']['category_id'];
      $skills     =  $_POST['SearchEmployee']['skills'];
      $experience =  $_POST['SearchEmployee']['experience'];

      $model = SearchEmployee::model()->find(array(
      'select' => array('*'), "condition" => "category_id=$category AND key_skills like '%$skills%' AND experience=$experience",
      ));

      $this->render('search',$model);
  }

  /*Getting Data From Search Form For Processing */
  $this->render('search', array('model' => $model));
}

In View Section: search.php//To display and search the desired result

<div class="row">
   <?php echo $form->labelEx($model,'Category'); ?>
   <?php echo $form->dropDownList($model,'category_id',$list,array('empty' =>'(Select a Category')); ?>
   <?php echo $form->error($model,'category'); ?>
</div>

  <div class="row">
<?php echo $form->labelEx($model,'Experience'); ?>
    <?php echo $form->textField($model,'experience'); ?>
    <?php echo $form->error($model,'experience'); ?>    
</div>


<div class="row buttons">
  <?php echo CHtml::submitButton('Search'); ?>
</div>



<div class="view">

  <h1>Results </h1>

  <div class="view" id="id">

     <h1> Records Display </h1>

     <h4>Name:        <?php echo $form->labelEx($model,'name'); ?></h4>
     <h4>Skills:      <?php echo $form->labelEx($model,'experience'); ?></h4>
     <h4>Experience:  <?php echo $form->labelEx($model,'skills'); ?></h4>
     <h5>&nbsp;&nbsp ;<?php echo $form->labelEx('VIew Details'); ?></h5>
   </div>

 </div>

In view section I am using the search and view results option in the same form.I am getting the error after clicking the search button.Is this the correct way to do

8
  • 1
    The error usually shows a line number and a trace as well. Do you have that? Commented Apr 17, 2014 at 12:39
  • Nathan...its here in view page search.php '<?php echo $form->labelEx($model,'Category'); ?>' Commented Apr 17, 2014 at 12:41
  • By the way your "find" query looks very unsafe, if not invalid. You should study the correct way to use ActiveRecords Commented Apr 17, 2014 at 12:41
  • Your search.php shows a submit button but not "form" - are you sure everything is here? Commented Apr 17, 2014 at 12:43
  • yes...I am not following the conventions correcly,just have started to use yii. Commented Apr 17, 2014 at 12:44

2 Answers 2

1
$model = SearchEmployee::model()->find(array(
                  'select' => array('*'), "condition" => "category_id=$category AND
         key_skills like '%$skills%' AND experience=$experience",
));

There is error here. You can't use find in that way.

public function find($condition='',$params=array())

$model = SearchEmployee::model()->find("category_id=$category AND
         key_skills like '%$skills%' AND experience=$experience");

But better

$model = SearchEmployee::model()->find("category_id=:category AND key_skills like :skill AND experience=:experience", array(
    'category'=>$category,
    'skill'=>'%'.$skills.'%',
    'experience'=>$experience
));
Sign up to request clarification or add additional context in comments.

2 Comments

I would recommend new CDbCriteria - especially when you explain it to a new Yii user.
author it seems like i helped you, why didn't you marked my answer as decision?
0

Remove this line:

$this->render('search',$model);

First it's invalid because it should be:

$this->render('search', array('model' => $model));

But also it's unnecessary because you already have it lower in your code.

2 Comments

Nathan...now I am getting error "Call to a member function isAttributeRequired() on a non-object in C:\wamp\www\yii\framework\web\helpers\CHtml.php on line 1414".After above mentioned change
Like I said - you will get more errors after you fix this one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.