How To Display Cgridview A Column Wherein It Doesnt Belong To Any Model

Cgridview in yii wherein the column doesnt belong to any model as i have not stored the value in db … ie., am calculating daysleft using a select query wherein the expiry date is stored in db and currendate we get it easily … as daysleft is not a field in any of the entities used in db … …


in view in


<?php $this->widget('zii.widgets.grid.CGridView', array(

    'id'=>'employee-view-grid',

    'dataProvider'=>$model->search(),

    'filter'=>$model,

    'columns'=>array(

array('header'=>'Daysleft','value'=>array($model,'daysleft')), 

    ...........

 }


 public function daysleft($data,$row)

{


    $sql="SELECT  datediff(fsp.expirydate,CURDATE())as daysleft

         from tbl fsp

       WHERE ( fsp.pid = ".$data->pt->id." 

      AND fsp.mid=".$data->mid.")


      limit 3";

      $connection = Yii::app()->db;

     $command=$connection->createCommand($sql);

    $dts=$command->queryRow();


     return $dts;

}



Please let me know

can i display it in gridview … if not then what can i do

Hi

check this one


array('header'=>'Daysleft','value'=>'$data->daysleft()'),

In your model add this code


public function daysleft(){

    $sql="SELECT  datediff(fsp.expirydate,CURDATE())as daysleft

         from tbl fsp

       WHERE ( fsp.pid = ".$this->pt->id." 

      AND fsp.mid=".$this->mid.")

      limit 3";

    $connection = Yii::app()->db;

    $command=$connection->createCommand($sql);

    $dts=$command->queryRow();


     return $dts;

}

tip: you should use bind params when use createCommand instead genereta query manually (avoiding hucking injections)

ya thanks later il be using bind params but for know

i get this error

Property "Pt.id" is not defined.

As you post the Pt is a related model of the main model ($model)

Please post the raltions method of your main model to help you

ya exactly

is relation in main model

‘pt’ => array(self::BELONGS_TO, ‘ModelPt’, ‘ptid’),

What is the primary key of the ModelPt ?

post the ModelPt attributes

pk is ptid i did write late




$this->pt->ptid;




the error i recieve is

PHP warning

htmlspecialchars() expects parameter 1 to be string, array given

htmlspecialchars(array("daysleft" => "44"), 3, "UTF-8")…

i.e, am receiving only the one values it should be for eg foreach in a list having ptid and mid should have daysleft

ptid mid daysleft

1 … 1 …30

2 …1 … 10

1 … 10 … 1

Please let me know this … am losing my mind on this

So

instead of


WHERE ( fsp.pid = "...".$this->pt->id."..."

you should use


WHERE ( fsp.pid = "...".$this->pt->ptid."..."

also check first the values using var_dump (…); die(); like that


public function daysleft($data,$row)

{

var_dump ($this->pt->ptid);

var_dump ($this->pt->id);

die();

...

}

What are the results?


public function daysleft()

{

var_dump ($this->ptid);

var_dump ($this->mid);

die();

...

}

$data,$row passing gives error

without $data,$row it gave me exactly the correct result i.e., only 1 set of data

i need daysleft (which is different) for each record in the gridview any guidiance you suggest

if i wanna render it as griview it displays me

PHP warning

htmlspecialchars() expects parameter 1 to be string, array given

public static function encode($text)

102 {

103 return htmlspecialchars($text,ENT_QUOTES,Yii::app()->charset);//to this line it points

104 }

Hi again

why you want to pass $data,$row to method ?

Using $data->daysleft() has all the attributes for each row ($data is the current model foreach row)

Have you specific reason to pass the row into the model method ?

Hi,

i did is this way but still but yet not found please refer to this link My link