Mahdi Miad
Mahdi Miad

Reputation: 63

Call function at CGridView in yii

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:

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()',
            ),
    ),
)); ?>

So I can call that function and it is working but the problem is it is displaying first CGridView row only, because I need to display all waybill with their containers or cargo.

Upvotes: 2

Views: 2488

Answers (2)

Mahdi Miad
Mahdi Miad

Reputation: 63

Thanks Samuel, I got the solution, the problem was I wrote

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

instead of

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

So that was my problem, and now it is working nice

Upvotes: 0

Samuel Liew
Samuel Liew

Reputation: 79113

getRelatedContainer() is mis-spelled in your model:

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

Upvotes: 2

Related Questions