Quantcast
Channel: Yii Framework Forum
Viewing all articles
Browse latest Browse all 18717

Obtaining Data From Another Table Cgridview Widget

$
0
0
Hello,

I am currently trying to use the CGridView widget. I am posting as I am unsure as to how to get a column from another table (target_name from the target table) and have it displayed in the table used in CGridView. I have 3 tables; drug_has_target is my intermediate table, which is linked to drug and target. Currently, CGridView is using the drug_has_target (intermediate!) table. I would like to;

1) add an extra column in this widget

2) have the content of the extra column to be target_name (from table 2, an FK of intermediate table points to PK of this table)

I would like to know how to go about this ..how can I add a column and "fetch" the content from a different table which is linked. I've tried different options which I saw online, but they didn't work ... would say that drug_has_target.target.target_id is not defined (or something along these lines). Curious to know if I need to create relations +attribute label in the drug_has_target model script in order to add this to the CGridView widget.

Drug (table 1):
drug_id (PK)
drug_name

Target (table 2):
target_id (PK)
target_name

drug_has_target (table 3, *intermediate table*):
drug_id (FK1)
target_id (FK2)

Current relations:
//drug_has_target.php (model)
public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'drug' => array(self::BELONGS_TO, 'Drug', 'drug_id'),
			'target' => array(self::BELONGS_TO, 'Target', 'target_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'drug_id' => 'Drug',
			'target_id' => 'Target',
		);
	}




I don't really know what I am doing (newbie), so if you can manage to be explicit with your solution, that would be VERY much appreciated!

Thank you!
Tanya

Viewing all articles
Browse latest Browse all 18717

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>