Store Related Id's

I need the Tag id’s which are related to the Daytrip which is loaded in the actionUpdate.

Below you will find my code. But the problem is that the $this->tags is empty. I’ve done a print_r($this->tags) and the results is Array( ).

The relation to the Tag model is defined as follow:


'tags' => array(self::MANY_MANY, 'Tag', 'daytrip_tag(daytrip_id, tag_id)'),

Any help or suggestions are appreaciated

Controller:


	public function actionUpdate()

	{

		$model=$this->loadModel();


		Daytrip::model()->getDaytripTags();


		if(isset($_POST['Daytrip']))

		{

			$model->attributes=$_POST['Daytrip'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->id_daytrip));

		}


		$this->render('update',array(

			'model'=>$model,

		));

	}

Model:


	public function getDaytripTags()

	{

		$data=array();

		foreach ($this->tags as $n => $tag) //$this->tags is empty

			$data[] = array('id'=>$tag->id_tag, 'text'=>$tag->tag);

		

		$this->tagIds = CJSON::encode($data);

		

		return $this->tagIds;

	}

Correct me if I’m wrong but $this in getDaytripTags isn’t populated with an ID. Not sure if I’m reading your code right but shouldn’t you be using the loaded model instance?




$model = $this->loadModel(); 

$model->getDaytripTags();



Matt

That code looks odd. How do you know which record you are dealing with?

Where do you pass the ID?

Normally it looks like this:


public function actionUpdate($id) {

    

    $model=$this->loadModel($id);

    ....

i use the code below. generateg with the gii tool


	public function loadModel()

	{

		if($this->_model===null)

		{

			if(isset($_GET['id']))

				$this->_model=Daytrip::model()->findbyPk($_GET['id']);

			if($this->_model===null)

				throw new CHttpException(404,'The requested page does not exist.');

		}

		return $this->_model;

	}