Viewing An Intermediate Table In Yii

Hello,

I have recently joined 2 tables into an intermediate table called drug_has_target (see below). I would like to know if there’s a way where I can make sure this intermediate table works (ex. drug_id has a target_id). My thought was to echo a row on a view page. To do so, I have been trying different suggestions found online, but they do not work (the view page in question does not load, but gives a white/empty page).

I currently have 3 tables, 2 have models, and the 3rd is an intermediate table (has no model associated with it).

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)

Relations are as follows;




//Drug.php


return array(

'targets' => array(self::MANY_MANY, 'Target', 'drug_has_target(target_id, drug_id)')

)




//Target.php


return array(

		'drugs' => array(self::MANY_MANY, 'Drug', 'drug_has_target(drug_id, target_id)')

		)




What I have attempted is;




//DrugController.php 


class DrugController extends Controller

{

$model = Drug::model()->with('targets')->findByPk($id);

	public function actionIndex()

	{

		$this->render('index', $model);

	}




For the view file, I either left it unchanged, or tried the following;

//Drug (/views/Drug/index.php)




<?php echo $model; ?>



I know the table exists because Gii is willing to generate a Controller and View for the intermediate table drug_has_target (but it won’t generate a model for it). What I would like to make sure is that drug_id is being associated with target_id, preferably visually. My current efforts have the Drug page (/Drug/index.php) showing up blank when I attempt to view the contents as described above, and when I remove that added script, the page has content again.

I am new, so it’s possible that I am “missing” an important concept somewhere!

Thank you!

Tanya

You’ll need a model for the intermediate table. Once you have it you can generate its CRUD controller and views.