Dynamic relations

My League class


 * This is the model class for table "league".


 * The followings are the available columns in table 'league':

 * @property integer $id

 * @property string $name

 * @property integer $size

 * @property integer $exchange_down

 * @property integer $id_season


class League extends CActiveRecord


	public $id_season = 0;


	public function relations()


		return array(

			'players' => array(self::HAS_MANY, 'PlayerStat', 'id_league',

			'condition'=>'id_season = ' . $this->id_season),



My Season class

    public function getLeagues()



            foreach(League::model()->findAll() AS $League)


                //the ID I assign here is always ignored by League which always uses the default ID of 0

                //what can I do about this?

                $League->id_season = $this->id;

                $this->_leagues[] = $League;


        return $this->_leagues;


How do I get League/CActiveRecord to use the $id_season I assign it, rather than the default: 0? I’m guessing the problem caused by static methods.

Remove the condition from the relation definition and instead pass it to the findAll() method. You could also define a scope that manipulates the criteria accordingly. Then you can do:


If ‘id_season’ is a column in the table ‘league’, you should not declare it explicitly as a member variable in the League model. ActiveRecord will create an attribute for it automatically. I believe you are interfering with this.

Remove this line:


public $id_season = 0;

If you need to default it to 0, do it in the League constructor, or do it in you database schema. Personally null seems like a better default.

Also, I assume you’re "save"ing or "update"ing the $League variable after you modify it…

Thanks for the feedback guys.