Last active
August 29, 2015 13:57
-
-
Save yoshihiko-k/9417958 to your computer and use it in GitHub Desktop.
cakephpでコンディションを作成する時に楽をしたいと思って作ってます.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AppModel extends Model { | |
protected $field_alias = array( | |
'short'=>'longlonglonglonglong', | |
); | |
protected function fieldAlias( $field ){ | |
if( isset( $this->field_alias[ $field ] ) ) $field = $this->field_alias[ $field ]; | |
return $field; | |
} | |
/** cake conditions maker | |
[exsample] | |
$this->cond( | |
array( 'id'=>$id, 'datetime'=>array( $d1,$d2 ) ), | |
array('or'=> array( array( 'any'=>1 ), array( 'any'=>2, 'some'=>1 ) ) , | |
array('not'=>array('disabled'=>1)) | |
); | |
equal | |
array_merge_recurcive( | |
$this->condField( 'id', $id ), | |
$this->condDatetime( $d1, $d2 ), | |
$this->condOr( array( 'any'=>1 ), array( 'any'=>2, 'some'=>1 ) ), | |
$this->condNot( array( 'disabled'=>1 ) ), | |
); | |
[return] | |
array( 'conditions'=> array( | |
'Alias.id'=>$id, | |
'Alias.date Between ? and ?'=> array( $d1, $d2 ), | |
// result $this->condTest(), | |
'OR'=> array( | |
'Alias.any'=>1, | |
array( | |
'Alias.any'=>2, | |
'Alias.some'=>1 | |
) | |
), | |
'NOT'=> array( | |
'Alias.disabled'=>1 | |
) | |
) ); | |
*/ | |
protected function cond(){ | |
$ret = array(); | |
$this->_cond( func_get_args(), $ret ); | |
return array( 'conditions'=>$ret ); | |
} | |
protected function _cond( $arr, &$ret ){ | |
foreach( $arr as $k=>$v ){ | |
if( !is_numeric($k) ){ | |
$name = $k; | |
$args = is_array($v) ? $v : array($v); | |
} | |
else if( is_array($v) ){ | |
$this->_cond( $v, $ret ); | |
continue; | |
} | |
else { | |
$name = $v; | |
$args = array(); | |
} | |
$func = $this->arg2FuncName( $name ); | |
if( method_exists($this, $func) ){ | |
$_ret = call_user_func_array( array( $this, $func ), $args ); | |
} else { | |
$_ret = $this->condField( Inflector::underscore( $name ), $args ); | |
} | |
$ret = array_merge_recursive( $ret, $_ret ); | |
} | |
} | |
protected function arg2FuncName( $val ){ | |
return 'cond'.Inflector::camelize( $val ); | |
} | |
// make OR | |
protected function condOr(){ | |
$ret = array(); | |
$this->_cond( func_get_args(), $ret ); | |
return array( array('OR'=>$ret) ); | |
} | |
// make Not | |
protected function condNot(){ | |
$ret = array(); | |
$this->_cond( func_get_args(), $ret ); | |
return array( array('Not'=>$ret) ); | |
} | |
// make Between | |
protected function condBetween( $field, $p1, $p2, $date_format = null ){ | |
if( $date_format ){ | |
$p1 = date( $date_format, strtotime( $p1 ) ); | |
$p2 = date( $date_format, strtotime( $p2 ) ); | |
} | |
$field = $this->fieldAlias( $field ); | |
return array( $this->name.'.'.$field.' BETWEEN ? AND ?' => array( $p1, $p2 ) ); | |
} | |
// make Like | |
protected function condLike( $field, $val, $format = "%_%" ){ | |
$field = $this->fieldAlias( $field ); | |
return array( $this->name.'.'.$field.' LIKE' => str_replace('_', $val, $format) ); | |
} | |
// make any field | |
protected function condField( $field, $val ){ | |
if( $val == array() ) return array(); | |
if( is_array( $val ) && count( $val ) == 1 ) $val = $val[0]; | |
if( $val == null ) return array(); | |
$field = $this->fieldAlias( $field ); | |
return array( $this->name.'.'.$field => $val ); | |
} | |
// make date | |
protected function condDate( $d1 = null, $d2 = null, $field = 'date', $format = 'Y-m-d' ){ | |
if( !$d1 ) return array(); | |
if( $d1 && $d2 ){ | |
return $this->condBetween( $field, $d1, $d2, $format ); | |
} | |
$field = $this->fieldAlias( $field ); | |
return array( $this->name.'.'.$field => date( $format, strtotime( $d1 ) ) ); | |
} | |
// make datetime | |
protected function condDatetime( $d1 = null, $d2 = null, $field = 'date' ){ | |
return $this->condDate( $d1, $d2, $field, 'Y-m-d H:i:s' ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
実際はこんな感じで使うと凄く楽になる。
public function findTargetDate( $some, $any, $date ){
return $this->find( 'all', $this->cond( compact( 'some', 'any', 'date' ) ) );
}