在 yii 中的 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 行,因为我需要显示所有运单及其集装箱或货物。

getRelatedContainer()在您的

模型中拼写错误:

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

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

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

而不是

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

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

最新更新