intruder
(Intruder Home)
1
Hi guys! Help a beginner.
How to display the detail table?
Table: Tab1
tab1_id (PK)
tab1_name
Table: Tab2
tab2_id (PK)
tab2_name
tab2_link_tab1
Model Tab1
public $owner;
public function relations()
{
return array(
'var_svyaz' => array(self::HAS_MANY, 'Tab2', 'tab2_link_tab1'),
);
}
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('tab1_id',$this->tab1_id);
$criteria->compare('tab1_name',$this->tab1_name,true);
$criteria->with = array('var_svyaz');
$criteria->compare( 'var_svyaz.tab2_link_tab1', $this->owner, true );
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
View Tab1
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'tab1_id',
'tab1_name',
),
)); ?>
<!-- ------------------- the detail table ------------------------------------------------ -->
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'clients-grid',
'dataProvider'=>$model->search(),
'columns'=>array(
array(
'name' => 'the detail table',
'value'=>'$data->var_svyaz->tab2_name', // Dont work
),
)); ?>
I want to display the detail table as in the attachment
georaldc
(Georaldc)
3
This relation:
'var_svyaz' => array(self::HAS_MANY, 'Tab2', 'tab2_link_tab1'),
would give you an array of objects, not a single object so this:
array(
'name' => 'the detail table',
'value'=>'$data->var_svyaz->tab2_name', // Dont work
),
would not work because $data->var_syvaz would be an array. You should have gotten a Notice error if you have error reporting enabled.
intruder
(Intruder Home)
4
Thank you for your attention to my problem.
With an array of I guessed already, as if writing
'value' => '$ data-> var_svyaz [0] -> tab2_name',
I get the first element of the array.
And how to get all the elements?
If I make through the function - displays all on one line.
'value' => '$ data-> rawTest2 ($ data-> var_svyaz)',
public static function rawTest2 ($ plans) {
$ tmp = '<div class = "raw2">';
foreach ($ plans as $ plan) {
$ tmp = $ tmp. $ plan-> tab2_name. '<br/>';
}
$ tmp = $ tmp. '</ div>';
return $ tmp;
}
intruder
(Intruder Home)
5
solved the problem as follows: changed $dataProvider:
View Tab1
<?php
$config = array();
$dataProvider = new CActiveDataProvider('Tab2', array('data' => $model->var_svyaz));
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'clients-grid',
//'dataProvider'=>$model->search(),
'dataProvider'=>$dataProvider,
'columns'=>array(
array(
'header' => '№',
'value' => '$row+1',
),
array(
'name' => 'Detail table',
'value' => '$data->tab2_name',
),
array(
'name' => 'ID',
'value' => '$data->tab2_id',
),
array(
'class'=>'CButtonColumn'
, 'viewButtonUrl'=>'Yii::app()->createUrl("/tab2/view", array("id"=>$data["tab2_id"]))'
, 'updateButtonUrl'=>'Yii::app()->createUrl("/tab2/update", array("id"=>$data["tab2_id"]))'
, 'deleteButtonUrl'=>'Yii::app()->createUrl("/tab2/delete", array("id"=>$data["tab2_id"]))'
)
),
)); ?>
thanks to all