ask about search

hi all, i want ask about _search.php

i make a dropdownlist in _search.php and in db the status like this

null -> status new

1 -> status approved

0 -> status not approved

then i try search with drop down i choose, when i choose approved and not approved it show all the data when it approved or not approved, but when i choose new the search show the all data in db. can someone help me?

thanks.

the function


function getStatus()

	{

		return array(null=>'NEW',1=>'Approved',0=>'Not Approved');

	}

and the code in _search.php


<div class="row">

		<?php echo $form->label($model,'status'); ?>

		<?php echo $form->dropDownList($model,'status',Controller::getStatus()); ?>

	</div>

You will find something like this in your model code:




	public function search()

	{

		$criteria=new CDbCriteria;

		....

		$criteria->compare('status',$this->status);

		....



CDbCriteria::compare() will create WHERE clause IF the given search parameter IS NOT empty (or null? I don’t know). So comparing ‘status’ with ‘1’ or ‘0’ will work. But it will ignore an empty search parameter and will not create the corresponding WHERE clause, meaning we don’t care about it. This is why your search for ‘null’ fails.

In other words, there is a mismatch between the values in DB and the search parameter:

In the DB:

  • null … status new

  • 1 … status approved

  • 0 … status not approved

And in the search parameter:

  • empty … don’t care

  • 1 … status approved

  • 0 … status not approved

I would modify the values of search parameter (and the contents of the dropDownList accordingly):

  • empty … don’t care

  • 2 … status new

  • 1 … status approved

  • 0 … status not approved

And modify the search method like this:




	public function search()

	{

		$criteria=new CDbCriteria;

		....

		if ($this->status == 2)

			$criteria->addCondition('status IS NULL');

		else

			$criteria->compare('status',$this->status);

		....



thanks softark, it’s working now ^^