powers
(Amohamed86)
November 19, 2012, 2:09pm
1
I joined a table now I have some empty columns, which is perfect because I want to search and display those empty columns. The only problem is how do you search for an empty column on CGridView because it’s not 0… I tried
'value' => '(empty($data->ID)) ? "0" : $data->ID' , // I want to show all data with "0"
But it only searches models, so any text that’s not in the DB cannot be searched. if I search 0 I get no results.
Thanks
seenivasan
(Chellamnivas)
November 19, 2012, 4:53pm
2
Dear ItsYII
We can overide the afterFind method in AR.
public function afterFind()
{
parent::afterFind();
if(!isset($this->ID))
$this->ID=0;
}
Regards.
Bianca
(Biancajsen)
November 20, 2012, 3:00am
3
I joined a table now I have some empty columns, which is perfect because I want to search and display those empty columns. The only problem is how do you search for an empty column on CGridView because it’s not 0… I tried
'value' => '(empty($data->ID)) ? "0" : $data->ID' , // I want to show all data with "0"
But it only searches models, so any text that’s not in the DB cannot be searched. if I search 0 I get no results.
Thanks
Try
'value'=>'$data->ID!==null?$data->ID:"0"
powers
(Amohamed86)
November 20, 2012, 6:22am
4
@Jimlam and @seenivasan thank you all for the replies
Both solutions produce the same results I already have, it prints 0 on the column but it cannot be searched…
Cheers
Bianca
(Biancajsen)
November 20, 2012, 7:45am
5
@Jimlam and @seenivasan thank you all for the replies
Both solutions produce the same results I already have, it prints 0 on the column but it cannot be searched…
Cheers
Is ID an attribute of the model being searched? I guess that it is not. It is an attribute of the related table.
I think this wiki can help you:
http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview/
powers
(Amohamed86)
November 20, 2012, 8:16am
6
Can it be both ID is an attribute of the model yes, but the empty ID’s are the attribute of the related table. I read the Wiki and it mostly explains joining tables wich I successfully have, nothing to do with searching empty fields. I suppose you are right because the one I’m trying to search is an attribute of the related table. I can search through all related table attributes just fine, only empty values I can’t.
Thanks
reza.m
(Reza Mms)
November 20, 2012, 10:20am
7
it can be done with something like this in your search method:
if($this->column=='0')
$criteria->addCondition('column is null');
else
$criteria->compare('column',$this->column);
seenivasan
(Chellamnivas)
November 20, 2012, 10:57am
8
Dear ItsYII
You are absolutely correct.
And the solution is elegantly given by Reza M .
if(isset($this->ID))
{
if($this->weight==='0')
$criteria->addCondition("ID IS NULL OR ID=0");
else $criteria->compare('ID',$this->ID);
}
powers
(Amohamed86)
November 20, 2012, 12:53pm
9
it can be done with something like this in your search method:
if($this->column=='0')
$criteria->addCondition('column is null');
else
$criteria->compare('column',$this->column);
@Reza
Yep that’s done it, thank you very much.
Appreciate all the help from everyone
jmunozdev
(Jmunoz Dev)
March 7, 2017, 3:04pm
10
If you need to do this for every field in your model you can use the attributelabels() function to get it done in a very easy way:
foreach ($this->attributeLabels() as $key => $value) {
if (strtolower($this->$key)=='0')
$criteria->addCondition($key.' is null');
else
$criteria->compare('LOWER('.$key.')', strtolower($this->$key), true); //last parameter is optative
}