<?php foreach ($data->targets as $target):?>
<div class="drug-target">
<?php echo "{$target->id}: {$target->name}";?>
</div>
<?php endforeach; ?>
Matt
Posted 18 March 2013 - 01:48 PM
<?php foreach ($data->targets as $target):?>
<div class="drug-target">
<?php echo "{$target->id}: {$target->name}";?>
</div>
<?php endforeach; ?>
Posted 19 March 2013 - 12:05 PM
changes.png (5.11K)
//Drug.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(
'targets' => array(self::MANY_MANY, 'Target', 'Drug_has_target(drug_id, target_id)')
);
}
public function targetsToString()
{
$targets = $this->targets;
if ($targets)
{
$string = '';
foreach($targets as $target) {
$string .= $targets->target_id . ', ';
}
return substr($string,0,strlen($string)-1);
}
return null;
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'drug_name' => 'Drug Name',
'drug_id' => 'Drug ID',
);
}
//Admin.php (for Drug)
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'drug-targets-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'drug_id',
'drug_name',
array(
'header' => 'Targets',
'value' => '$data->targetsToString()'
),
array(
'class'=>'CButtonColumn',
),
),
)); ?>
waterloomatt, on 18 March 2013 - 01:34 PM, said:
public function targetsToString()
{
$targets = $this->targets;
if ($targets)
{
$string = '';
foreach($targets as $target) {
$string .= $targets->name . ', ';
}
return substr($string,0,strlen($string)-1);
}
return null;
}
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'drug-targets-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'name',
array(
'header' => 'Targets',
'value' => '$data->targetsToString()'
),
array(
'class'=>'CButtonColumn',
),
),
)); ?>
Posted 19 March 2013 - 12:18 PM
//Admin.php
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'drug-targets-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'drug_id',
'drug_name',
array(
'header' => 'Target ID',
value'=>'$this->grid->getTARGET()->renderPartial(\'_viewTARGET_ID\', array(\'data\'=>$data),true);',
),
array(
'class'=>'CButtonColumn',
),
),
)); ?>
waterloomatt, on 18 March 2013 - 01:48 PM, said:
<?php foreach ($data->targets as $target):?>
<div class="drug-target">
<?php echo "{$target->id}: {$target->name}";?>
</div>
<?php endforeach; ?>
Posted 19 March 2013 - 10:39 PM
//Admin.php
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'drug-targets-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'drug_id',
'drug_name',
array(
'header' => 'Target ID',
'type' => 'raw',
'value'=>'$this->grid->getOwner()->renderPartial("_drug_target", array("data"=>$data), true);',
),
array(
'class'=>'CButtonColumn',
)
)
)); ?>
// $this->grid->getOwner() returns the owner (usually a controller) of the grid. Then, we're using the controller to render a partial view of the 'targets' relation.
// _drug_target is the name of the partial view file to render. I've created and attached it to this post; drop it in the same folder and the grid. (views/drug ??)
// We're passing a $data object (Drug model) to the view and then looping over it's 'targets' relation
_drug_target.php (154bytes)
Posted 20 March 2013 - 06:24 AM
waterloomatt, on 19 March 2013 - 10:39 PM, said:
//Admin.php
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'drug-targets-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'drug_id',
'drug_name',
array(
'header' => 'Target ID',
'type' => 'raw',
'value'=>'$this->grid->getOwner()->renderPartial("_drug_target", array("data"=>$data), true);',
),
array(
'class'=>'CButtonColumn',
)
)
)); ?>
// $this->grid->getOwner() returns the owner (usually a controller) of the grid. Then, we're using the controller to render a partial view of the 'targets' relation.
// _drug_target is the name of the partial view file to render. I've created and attached it to this post; drop it in the same folder and the grid. (views/drug ??)
// We're passing a $data object (Drug model) to the view and then looping over it's 'targets' relation
Posted 20 March 2013 - 07:41 AM
Posted 20 March 2013 - 08:11 AM
waterloomatt, on 20 March 2013 - 07:41 AM, said:
Drug.php (4.01K)
Target.php (2.37K)
DrugController.php (4.09K)
admin.php (1.59K)
_search.php (1.22K)
view.php (815bytes)
_view.php (827bytes)
index.php (390bytes)
_drug_target.php (169bytes)
_form.php (1.59K)
update.php (570bytes)
create.php (353bytes)
Posted 20 March 2013 - 12:49 PM
medicine-app.jpg (61.47K)
medicine.zip (326.57K)
--
-- Structure for table `drug`
--
DROP TABLE IF EXISTS `drug`;
CREATE TABLE IF NOT EXISTS `drug` (
`drug_id` int(11) NOT NULL AUTO_INCREMENT,
`drug_name` varchar(100) NOT NULL,
`drug_indication` varchar(1700) NOT NULL,
`drug_synonym` varchar(120) NOT NULL,
`drug_brand` varchar(40) NOT NULL,
PRIMARY KEY (`drug_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
--
-- Data for table `drug`
--
INSERT INTO `drug` (`drug_id`, `drug_name`, `drug_indication`, `drug_synonym`, `drug_brand`) VALUES
('1', 'Test', 'Test', 'Test', 'Test');
--
-- Structure for table `target`
--
DROP TABLE IF EXISTS `target`;
CREATE TABLE IF NOT EXISTS `target` (
`target_id` int(11) NOT NULL AUTO_INCREMENT,
`target_name` varchar(70) NOT NULL,
PRIMARY KEY (`target_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
--
-- Data for table `target`
--
INSERT INTO `target` (`target_id`, `target_name`) VALUES
('1', 'target1'),
('2', 'target2');
--
-- Structure for table `drug_has_target`
--
DROP TABLE IF EXISTS `drug_has_target`;
CREATE TABLE IF NOT EXISTS `drug_has_target` (
`drug_id` int(11) NOT NULL,
`target_id` int(11) NOT NULL,
PRIMARY KEY (`drug_id`,`target_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Data for table `drug_has_target`
--
INSERT INTO `drug_has_target` (`drug_id`, `target_id`) VALUES
('1', '1'),
('1', '2');
Posted 20 March 2013 - 12:54 PM
<?php foreach ($data->targets as $target):?> <div class="drug-target"> <?php echo $target->target_id?. ': '. $target->target_name;> </div> <?php endforeach; ?> // Notice this line: <?php echo $target->target_id?. ': '. $target->target_name;> <?php foreach ($data->targets as $target):?> <div class="drug-target"> <?php echo $target->id . ': '. $target->name;?> </div> <?php endforeach; ?>
Posted 21 March 2013 - 08:14 AM
waterloomatt, on 20 March 2013 - 12:49 PM, said:
--
-- Structure for table `drug`
--
DROP TABLE IF EXISTS `drug`;
CREATE TABLE IF NOT EXISTS `drug` (
`drug_id` int(11) NOT NULL AUTO_INCREMENT,
`drug_name` varchar(100) NOT NULL,
`drug_indication` varchar(1700) NOT NULL,
`drug_synonym` varchar(120) NOT NULL,
`drug_brand` varchar(40) NOT NULL,
PRIMARY KEY (`drug_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
--
-- Data for table `drug`
--
INSERT INTO `drug` (`drug_id`, `drug_name`, `drug_indication`, `drug_synonym`, `drug_brand`) VALUES
('1', 'Test', 'Test', 'Test', 'Test');
--
-- Structure for table `target`
--
DROP TABLE IF EXISTS `target`;
CREATE TABLE IF NOT EXISTS `target` (
`target_id` int(11) NOT NULL AUTO_INCREMENT,
`target_name` varchar(70) NOT NULL,
PRIMARY KEY (`target_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
--
-- Data for table `target`
--
INSERT INTO `target` (`target_id`, `target_name`) VALUES
('1', 'target1'),
('2', 'target2');
--
-- Structure for table `drug_has_target`
--
DROP TABLE IF EXISTS `drug_has_target`;
CREATE TABLE IF NOT EXISTS `drug_has_target` (
`drug_id` int(11) NOT NULL,
`target_id` int(11) NOT NULL,
PRIMARY KEY (`drug_id`,`target_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Data for table `drug_has_target`
--
INSERT INTO `drug_has_target` (`drug_id`, `target_id`) VALUES
('1', '1'),
('1', '2');
Posted 21 March 2013 - 08:33 AM
Posted 21 March 2013 - 11:35 AM
Quote