简体   繁体   中英

php yii CGridView add column from different model

The following I have in my admin.php view file, model is Components

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'components-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    /*'attributes'=>array(
        array(
         'label'=>'Asset Number',
                'value'=>"help",
        ),
    ),*/
    'columns'=>array(
        'component_id',

        'description',
        'version',
        'serial_no',
        'purchase_date',
        /*'disposal_date',

        'model',
        'nol',
        'manufacturer',
        'medium',
        */
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); ?>

I would like to add a extra column called asset number which is a field in another model FixedAsset . The two tables are linked together with their PK in the model ComAsset . What I ideally an trying to achieve is check each record in Components and see if it linked via the component_id if so then pull out the original_asset_number from FixedAsset using fixed_asset_id from ComAsset

relations

components - comasset = 1:1

fixedasset - comasset = 1:M

Assuming the relationships are properly setup in the respective models you should be able to access the related information via $data->relationshipName in the CGridView . If I got the relationships right from your description the value part for this column should look something like this: $data->comAsset->fixedAsset->name .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM