Limit specific columns in a table

I have a list of result-times that athletes have made.

In my CGridview advance search, i want to be able to check a checkbox to only display the best time that every athlete have made.

Checkbox:

Alternative 1: Show all times

Alternative 2: Show only the best time for every athlete

In the "Result" table all the result_times are stored, each result_time has an athlete_id. So I need to limit result_time to 1 for each athlete_id when I check the checkbox somehow.

Any ideas?

Hi. I think it’s quite easy. This is how I’d do it server-side:

  • Add a virtual attribute for that checkbox input field.

  • Add it to the safe array in the model’s rules.

  • In the search method, perform a check on that attribute, depending if true or false, perform the relevant query

Thanks for the fast reply,

This is how i did it, need a little help with the search method tho.




public $checkbox;


public function rules()

	{

		array('..., checkbox', 'safe', 'on'=>'search'),

		);

	}


public function search()

{

	if ($this->checkbox)

        {

        $criteria->limit = '';

        }

      // I guess the else-statement isn't nessesary?

}



I was thinking more of this kind of Result model:


public $onlyBestTime;

public $bestTime;

…

public function search()

{

    $criteria=new CDbCriteria;

    if($this->onlyBestTime) {

        $criteria->select = 'MIN(resultTime) AS bestTime, t.*';

        $criteria->with = array('athlete');

        $criteria->group = 'athleteId';

    }

    …

    $dp = new CActiveDataProvider($this, array('criteria'=>$criteria));

    // following line to give proper total number of items

    // only needed when we use with / group by

    $dp->setTotalItemCount(count($this->findAll($criteria)));

    return $dp;

}

PS I don’t think limit will work, or I don’t see how.

Can’t get it to work,

How can I combine:




$dp = new CActiveDataProvider($this, array('criteria'=>$criteria));

    // following line to give proper total number of items

    // only needed when we use with / group by

    $dp->setTotalItemCount(count($this->findAll($criteria)));

    return $dp;



with this?




return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));



and what does the "t" do in $criteria->select?

Use one of them only. Try with your syntax, you’ll probably find the results number is incorrect, and then you’ll use the code I’ve suggested ;)

For t, it refers to the model’s table, here, Result. Anyway you can use *, depends on the columns you have in your grid.

Sorry for being such a problem, Im pretty new with yii and trying to get the hang of it.

Still getting an sql-error.

This is my result model:




public $onlyBestTime;

public $bestTime;


public function rules()

	{

			array('..., onlyBestTime, bestTime', 'safe', 'on'=>'search'),

		);

	}


public function relations()

	{

		return array(

			'athlete' => array(self::BELONGS_TO, 'Athlete', 'athlete_id'),

		);

	}


public function search()

	{

		$criteria=new CDbCriteria;

		

		if($this->onlyBestTime) {

        		$criteria->select = 'MIN(result_time) AS bestTime, t.*';

        		$criteria->with = array('athlete');

        		$criteria->group = 'athlete_id';

    		}

		

		$criteria->order = 'result_time ASC';

		$criteria->compare('...');


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,));

			setTotalItemCount(count($this->findAll($criteria)));

	}



And this is the _search view:




<div class="row">

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

		<?php echo $form->checkBox($model,'onlyBestTime'); ?>

	</div>



Here is a part of the error:




CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 209 General SQL Server error: Check messages from the SQL Server [209] (severity 16) [(null)]. The SQL statement executed was: SELECT TOP 10 [t].[result_id] AS [t0_c0], [t].[athlete_id] AS [t0_c1], [t].[event_code] AS [t0_c2], [t].[competition_id] AS [t0_c3], [t].[result_time] AS [t0_c4], [t].[result_date] AS [t0_c5], [t].[round_type] AS [t0_c6], [t].[athlete_pos] AS [t0_c7], [t].[team_id] AS [t0_c8], [t].[relay_time] AS [t0_c9], [t].[event_order_number] AS [t0_c10], [t].[split_time] AS [t0_c11], [athlete].



See any obvious errors?