php - Why Yii CDbcriteria filtering results before any actions? -


i changed code in model:

$criteria->compare('ring',$this->ring,true); 

to this:

$criteria->compare('ring',date('y-m-d', strtotime($this->ring)),true); 

and load manage page empty filters see 1 record , if 'ring' of record 01.01.1970

i understand happens because strtotime($this->ring) return zero. why sends when field empty, , why didn`t happen before.

it`s works not preaty

if ($this->ring!=0){  $criteria->compare('ring',date('y-m-d', strtotime($this->ring)),true);  } 

strtotime() returns false if input not date, since null not date return false,

date() converts timestamp date, due php's weakly typed design false returned strotime() converted 0 , passed timestamp of 0 date() start of utc on 01.01.1970 passed compare()

now cdbcriteria::compare function not modify existing search condition if value null see http://www.yiiframework.com/doc/api/1.1/cdbcriteria#compare-detail documentation on $value

so in case

$criteria->compare('ring',$this->ring,true); 

compare did not modify search condition because $this->ring null whereas

$criteria->compare('ring',date('y-m-d', strtotime($this->ring)),true); 

will modify search condition value passed compare 01.01.1970

you can alternatively use virtual attributes in model avoid conditions logic in search

public function getformatedring(){   if(isset($this->ring))     return date('y-m-d', strtotime($this->ring));   else     return null } 

and use

 $criteria->compare('ring',$this->formatedring,true) 

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -