Adding A Relational Value To Clistview

Hello,

I currently have 3 tables; drug, target and drug_has_target (intermediate). When I go to the Drug view page, each entry found in the drug table is shown. However each drug is related to a target in a MANY-MANY relationship. I would really like to add "target_id" (PK) and "target_name" to the view for each entry.

Whenever I try to do so, I get one of 2 errors;

  1. an error saying drug.target is not defined

  2. a blank screen

I’d like to know if someone is aware of how to include the target id’s and target names to each drug entry view.

I hope this makes sense? I’ve included an image of how it’s set up now, and what I wish to do.

4007

now.png

Currently, my relationships are set-up as follows;




//Drug.php

 public function relations()

    {

        

        return array(

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

        );

    }


//Target.php

 public function relations()

    {

        

        return array(

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

        );

    }




Thank you!

Tanya

Dear Friend

You have defined MANY_MANY relationship.

In views/drug/_view.php, $data->targets returns array of objects.

In that case, we have to add the following lines to fetch all the related data.




foreach($data->targets as $target)

{

    echo "targetID: ".$target->id;

    echo "</br>";

    echo "target Name: ".$target->target_name;

}



or

You can add a grid.You can add these lines in _view.php




$this->widget('zii.widgets.grid.CGridView', array(

	'id'=>$data->id,

	'dataProvider'=>new CArrayDataProvider($data->targets),

	'columns'=>array(

		'id',

		'target_name'

	),

)); 



Regards.

u may also use a function in model and controller to get those :)

CONTROLLER:

in action create




$anyfieldname= URMODELNAME::model()->findAll();

$field=array();

		foreach($anyfieldname as $field)

		{

			$field[$field->id] = $field->name;

		}



Hello,

I tried using the code snippet which you shared with me (thank you for this). Doing what was described in the 1st code snippet, I got a blank screen with only "Drugs" (title of the page). (code I used below)




//_view.php

<?php

/* @var $this DrugController */

/* @var $data Drug */

?>


<div class="view">


	<b><?php echo CHtml::encode($data->getAttributeLabel('drug_id')); ?>:</b>

	<?php echo CHtml::link(CHtml::encode($data->drug_id), array('view', 'id'=>$data->drug_id)); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('drug_name')); ?>:</b>

	<?php echo CHtml::encode($data->drug_name); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('drug_indication')); ?>:</b>

	<?php echo CHtml::encode($data->drug_indication); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('drug_synonym')); ?>:</b>

	<?php echo CHtml::encode($data->drug_synonym); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('drug_brand')); ?>:</b>

	<?php echo CHtml::encode($data->drug_brand); ?>

	<br />


<?php

	foreach($data->targets as $target)

{

    echo "targetID: ".$target->target_id;

    echo "</br>";

    echo "target Name: ".$target->target_name;

}

        ),

)); ?>

</div>



Using the second code snippet gave me a bunch of error messages.

  1. Error: Property "Drug.id" is not defined.



//_view.php

<?php

/* @var $this DrugController */

/* @var $data Drug */

?>


<div class="view">


	<b><?php echo CHtml::encode($data->getAttributeLabel('drug_id')); ?>:</b>

	<?php echo CHtml::link(CHtml::encode($data->drug_id), array('view', 'id'=>$data->drug_id)); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('drug_name')); ?>:</b>

	<?php echo CHtml::encode($data->drug_name); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('drug_indication')); ?>:</b>

	<?php echo CHtml::encode($data->drug_indication); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('drug_synonym')); ?>:</b>

	<?php echo CHtml::encode($data->drug_synonym); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('drug_brand')); ?>:</b>

	<?php echo CHtml::encode($data->drug_brand); ?>

	<br />


<?php

$this->widget('zii.widgets.grid.CGridView', array(

        'id'=>$data->id,

        'dataProvider'=>new CArrayDataProvider($data->targets),

        'columns'=>array(

                'id',

                'target_name'

        ),

)); 	


 ?>

</div>



Am I doing something wrong? I am including these code snippets in the _view.php page for the Drug model …

Thank you,

Tanya

Hello,

I was wondering if you could confirm if I correctly understood how to apply what you suggested (thank you for taking the time to have replied to my post!).

  1. I’m unsure about where I placed $targets in actionCreate()

  2. Unsure about how to get this to show up in the drug view page for each drug entry




//DrugController.php

public function actionCreate()

	{

		$model=new Drug;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


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

		{

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

			if($model->save())

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

		}


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

			'model'=>$model,

		));


                $targets= Drug::model()->findAll();

                 $field=array();

                foreach($targets as $target)

                {

                        $target[$target->target_id] = $target->target_name;

                }

	}






Dear Tanya

$data refers to model Drug.

$target refers to model Target.

Try to remove the error creating attribute names and relace with correct ones.

For example




$this->widget('zii.widgets.grid.CGridView', array(

       // 'id'=>$data->id,//THIS IS GIVING ERROR.REPLACE WITH drug_id

           'id'=>$data=>drug_id,

        'dataProvider'=>new CArrayDataProvider($data->targets),

        'columns'=>array(

                'id',//ALSO CHECK WHETHER id or target_id.

                'target_name' //ALSO CHECK WHETHER target_name EXISTS IN Target MODEL.

        ),

));     



Hello seenivasan,

I have made the changes. I now get a blank white page that says "Drugs" (title of my page).

Tanya

Using the following snippet of code which I used in a partial view, I get the drug targets to show up. Problem is, they show up "outside" of the square (see attached image).

Now that my targets are showing, I’m wondering how I can get them to show up in the drug views? I have tried copy & pasting (then modifying) everything between <b></br> to get the word “target id” to show up under “drug brand”, but I get “Drug.target_id is not defined”.

If someone could help, that would be great (I’m new, and still trying to get stuff working :S)!

4012

Drugs.png




//_view.php


<?php

/* @var $this DrugController */

/* @var $data Drug */

?>


<div class="view">


	<b><?php echo CHtml::encode($data->getAttributeLabel('drug_id')); ?>:</b>

	<?php echo CHtml::link(CHtml::encode($data->drug_id), array('view', 'id'=>$data->drug_id)); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('drug_name')); ?>:</b>

	<?php echo CHtml::encode($data->drug_name); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('drug_indication')); ?>:</b>

	<?php echo CHtml::encode($data->drug_indication); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('drug_synonym')); ?>:</b>

	<?php echo CHtml::encode($data->drug_synonym); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('drug_brand')); ?>:</b>

	<?php echo CHtml::encode($data->drug_brand); ?>

	<br />




</div>




<?php foreach ($data->targets as $target): ?>

    <div class="drug-target">

        <?php echo $target->target_id /*. ': ' . $target->target_name.*/;  ?>

    </div>

<?php endforeach; ?>



Thank you,

Tanya

Never mind, SOLVED :)

Posted for anyone who’s interested …




<div class="view">


	<b><?php echo CHtml::encode($data->getAttributeLabel('drug_id')); ?>:</b>

	<?php echo CHtml::link(CHtml::encode($data->drug_id), array('view', 'id'=>$data->drug_id)); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('drug_name')); ?>:</b>

	<?php echo CHtml::encode($data->drug_name); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('drug_indication')); ?>:</b>

	<?php echo CHtml::encode($data->drug_indication); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('drug_synonym')); ?>:</b>

	<?php echo CHtml::encode($data->drug_synonym); ?>

	<br />


	<b> <?php echo CHtml::encode($data->getAttributeLabel('drug_brand')); ?>:</b>

	<?php echo CHtml::encode($data->drug_brand); ?>

	<br />


	

	<b>

	<?php foreach ($data->targets as $target): ?> 

	<?php echo CHtml::encode($target->getAttributeLabel('target_id')); ?>:</b>

	<?php echo CHtml::encode($target->target_id); ?>

	<?php endforeach; ?>

	<br />


</div>