/** * Initializes the widget. * This method is called by {@link CBaseController::createWidget} * and {@link CBaseController::beginWidget} after the widget's * properties have been initialized. */ public function init() { // check that the necessary info has been passed in to the widget if (!$this->hasModelRelation()) { throw new CException(Yii::t('yii', '{class} must specify "model" and "relation" values.', array('{class}' => get_class($this)))); } // set the name if there is an override //$this->resolveName(); // Sort out the type of relation, and set it up accordingly $relations = $this->model->relations(); // $key = Name of the Relation // $value[0] = Type of the Relation // $value[1] = Related Model // $value[2] = Related Field or Many_Many Table if ($details = $relations[$this->relation]) { $this->_relationType = $details[0]; // save the type of relation $this->_relatedModel = new $details[1](); // set up an model of the related kind $this->_relatedModelPk = $this->_relatedModel->tableSchema->primaryKey; // get the related model's primay key switch ($this->_relationType) { case 'CBelongsToRelation': $this->setupBelongsTo($details); break; case 'CHasOneRelation': throw new CException('CHasOneRelation not supported by SRelationWidget yet'); // todo handle HasOne relations //$this->setupHasOne($details); break; case 'CManyManyRelation': $this->setupManyMany($details); break; case 'CHasManyRelation': throw new CException('CHasManyRelation not supported by SRelationWidget yet'); // todo handle HasMany relations //$this->setupHasMany($details); break; default: throw new CException('Unknown relation type for SRelationWidget'); } } }