emmi
(Qshefa)
January 17, 2014, 11:24am
1
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
konapaz
(Konapaz)
January 17, 2014, 11:34am
2
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)
emmi
(Qshefa)
January 17, 2014, 11:43am
3
KonApaz:
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.
konapaz
(Konapaz)
January 17, 2014, 11:54am
4
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
emmi
(Qshefa)
January 17, 2014, 12:00pm
5
ya exactly
is relation in main model
‘pt’ => array(self::BELONGS_TO, ‘ModelPt’, ‘ptid’),
konapaz
(Konapaz)
January 17, 2014, 12:18pm
6
emmi:
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
emmi
(Qshefa)
January 18, 2014, 4:16am
7
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
emmi
(Qshefa)
January 18, 2014, 10:52am
8
Please let me know this … am losing my mind on this
konapaz
(Konapaz)
January 19, 2014, 10:07am
9
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?
emmi
(Qshefa)
January 20, 2014, 11:36am
10
KonApaz:
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 }
konapaz
(Konapaz)
January 20, 2014, 1:24pm
12
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 ?
emmi
(Qshefa)
January 21, 2014, 10:32am
13
KonApaz:
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