在yii中调用CGridView中的函数

时间:2014-01-16 10:49:40

标签: php yii model cgridview

Table operation (
    id INT PRIMERY,
    Name VARCHAR(50),
    Loading VOURCHAR(50),
)

Table Waybill (
    Id INT PRIMERY,
    name VARCHAR(50), 
    operation_id VOURCHAR(50), // this is a foreign key of 
)


Table container (
    Id INT PRIMERY,
    name VARCHAR(50),
    container_no VARCHAR(50),
    operation_id VARCHAR(50), // this is a foreign key of operation table
    waybill_id  VARCHAR(50), // this is a foreign key of waybill table
)


Table cargo (
    Id INT PRIMERY,
    name VARCHAR(50),
    description VARCHAR(50),
    operation_id  VARCHAR(50), // this is a foreign key of operation table
    waybill_id VARCHAR(50), // this is a foreign key of waybill table
)

PHP类:

class Waybill extends CActiveRecord  {

public function relations()
{ 
    return array(
         'operations' => array(self::BELONGS_TO, 'Operation', 'operation_id'),
         'containerHM' => array(self::HAS_MANY, 'Container', 'waybill_id'),
        'cargoHM' => array(self::HAS_MANY, 'Cargo', 'waybill_id'),
       );
}

// this function is to display all related containers at Waybil CGridView
    public function getRaltedContainer(){
               $result ='';
            if($this->operations->loading='with' ){
                $allContainers =''; 
                $containers = $this->containerHM ; 
               // containerHM  is a HAS_MANY relation between Waybill and Container 
                foreach($containers as $container){                
                   $allContainers .= $container->container_no." - "; 
                } 
                $result =  $allContainers;
            }  if($this->operations->loading='cargo'){
                $allCargo =''; 
                $cargos = $this->cargoHM ;
                foreach($cargos as $cargo){                
                   $allCargo .= $cargo->description." <br />"; 
                } 
                $result = $allCargo;
            }

            return $result;
        }
}

PHP:

<?php
// At CGridView I need to call like this
$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'waybill-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'ajaxUpdate'=>false, 
    'columns'=>array(
         'id',
          array(
                'header'=>'Items',
                'type'=>'raw',
                'value'=>'$data->getRelatedContainer()',
            ),
    ),
)); ?>

所以我可以调用该函数并且它正在工作,但问题是它只显示第一个CGridView行,因为我需要显示所有运单及其容器或货物。

2 个答案:

答案 0 :(得分:2)

getRelatedContainer()在您的模型中拼写错误:

// this function is to display all related containers at Waybil CGridView
public function getRaltedContainer(){

答案 1 :(得分:0)

谢谢塞缪尔, 我得到了解决方案,问题是我写的

$this->operations->loading='with'

而不是

$this->operations->loading =='with'

这就是我的问题,现在它工作得很好