Yii Framework Forum: Store Related Id's - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Store Related Id's Rate Topic: -----

#1 User is offline   bas_vdl 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 215
  • Joined: 01-April 09

Posted 23 January 2013 - 02:25 PM

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;
	}

0

#2 User is offline   waterloomatt 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 545
  • Joined: 09-April 10

Posted 23 January 2013 - 11:42 PM

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
0

#3 User is offline   outrage 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 313
  • Joined: 10-November 09
  • Location:Blackpool, United Kingdom

Posted 24 January 2013 - 11:12 AM

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);
    ....

0

#4 User is offline   bas_vdl 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 215
  • Joined: 01-April 09

Posted 24 January 2013 - 12:21 PM

View Postoutrage, on 24 January 2013 - 11:12 AM, said:

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;
	}

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users