i have a table Fighters (where i keep curent weight and league info)
and a table FightersUpdates (where i track all the changes about weight and league)
in AddMatch when i choose home fighter it checks in what league is at matchdate and populate away fighter’s list only with fighters from that league at that time.
and i don’t know how to do this.
public function relations() {
return array(
'fightersUpds' => array(self::HAS_MANY, 'FightersUpd', 'id_fighter'),
);
}
public function actionDynamicFighters()
{
// Find fighter's league at matchdate
$home=FightersUpd::model()->findByAttributes(array(
'id_fighter'=>(int) $_POST['Matches']['id_home']
),
'change_date<=:change_date',
array(':change_date'=>$_POST['Matches']['date']
));
// If there is no result
if(!$home)
{
// get current fighter's league
$home=Fighters::model()->findByPk((int) $_POST['Matches']['id_home']);
}
// Find all away fighters in home's league at matchday
$away=Fighters::model()->with(array('fightersUpds'=>array(
'condition'=>'change_date<=:change_date AND id_fighter!=:id_fighter',
'params'=>array(
':change_date'=>$_POST['Matches']['date'],
':id_fighter'=>(int) $_POST['Matches']['id_home']
))->findAll(array(
'order'=>'name',
'condition'=>'id_league=:id_league AND id!=:id',
'params'=>array(
':id_league'=>(int) $home->id_league,
':id'=>(int) $_POST['Matches']['id_home']
)));
$away=CHtml::listData($away,'id','name');
echo '<option value="">= Select Away Fighter =</option>';
foreach($away as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
/* THIS ONE IS WORKING BUT IT RETURNS FIGHTERS WITH PRESENT ID_LEAGUE OF COURSE
$away=Fighters::model()->findAll(array(
'order'=>'name',
'condition'=>'id_league=:id_league AND id!=:id',
'params'=>array(
':id_league'=>(int) $home->id_league,
':id'=>(int) $_POST['Matches']['id_home']
)));
$away=CHtml::listData($away,'id','name');
echo '<option value="">= Select Away Fighter =</option>';
foreach($away as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}
*/
}
I thought you wanted a list of related records to populate your fighters list but if you only want 1 record to be returned with your sample code, then just use find()