@ -50,8 +50,8 @@ abstract class SqlModel extends Model
*/
public function update ( $data , $where )
{
if ( is_int ( $where ) || $where === ( string ) ( int ) $where ) {
$where = $this -> identify ( $this -> key ) . '=' . ( int ) $where ;
if ( is_int ( $where ) || $where === ( string )( int ) $where ) {
$where = $this -> identify ( $this -> key ) . '=' . ( int ) $where ;
}
return parent :: update ( $data , $where );
}
@ -62,7 +62,7 @@ abstract class SqlModel extends Model
*/
public function delete ( $id )
{
$where = $this -> identify ( $this -> key ) . '=' . ( int ) $id ;
$where = $this -> identify ( $this -> key ) . '=' . ( int ) $id ;
return $this -> db -> delete ( $this -> table (), $where );
}
@ -87,7 +87,7 @@ abstract class SqlModel extends Model
for ( $i = 0 ; $i < count ( $params [ 'sort' ]); $i ++ ) {
$order = ( isset ( $params [ 'order' ][ $params [ 'sort' ][ $i ]]) && $params [ 'order' ][ $params [ 'sort' ][ $i ]] == 'desc' ) ? 'DESC' : 'ASC' ;
if ( in_array ( $params [ 'sort' ][ $i ], $sortable )) {
$order_list [] = $this -> identify ( $params [ 'sort' ][ $i ]) . ' ' . $order ;
$order_list [] = $this -> identify ( $params [ 'sort' ][ $i ]) . ' ' . $order ;
}
}
if ( $order_list ) {
@ -112,7 +112,7 @@ abstract class SqlModel extends Model
if ( $table_prefix ) {
$sql = $table_prefix . '.' ;
}
$sql .= $this -> identify ( $params [ 'qt' ]) . ' LIKE ' . $this -> quote ( '%' . $params [ 'q' ] . '%' );
$sql .= $this -> identify ( $params [ 'qt' ]) . ' LIKE ' . $this -> quote ( '%' . $params [ 'q' ] . '%' );
}
return $sql ;
}
@ -189,48 +189,43 @@ abstract class SqlModel extends Model
return $result ;
}
public function find ( $select , $distinct , $where , $order , $limit )
/**
* @ param $select array
* @ param $distinct string | bool
* @ param $where array @ ex array ( 'field=?' => $value , 'field=1' )
* @ param $order array @ ex array ( 'sort' => array ( 'field1' , 'field2' ), 'order' => array ( 'field2' => 'desc' ))
* @ param $limit string @ ex '30' or '30,30'
* @ param $heaving TODO
* @ param $group_by TODO
* @ param $sql_expression null | string
* @ param $sql_expression_params array
* @ param $cache_key CacheKey | null
* @ return SqlResultProvider
*/
public function find ( $select , $distinct , $where , $order , $limit , $heaving = null , $group_by = null , $sql_expression = null , $sql_expression_params = array (), $cache_key = null )
{
if ( ! $select ) {
$select = array ( new DbExpr ( '*' ));
}
foreach ( $select as $field => & $term ) {
if ( is_int ( $field )) {
if ( $term instanceof DbExpr ) {
$term = ( string ) $term ;
} else {
$term = $this -> db -> quoteIdentifier ( $term );
}
} else {
$term = $this -> db -> quoteIdentifier ( $field ) . ' as ' . $this -> db -> quoteIdentifier ( $term );
}
}
foreach ( $where as $cond => & $term ) {
if ( is_array ( $term )) {
$term = new DbExpr ( '(' . implode ( ',' , array_map ( function ( $item ){ return intval ( $item );}, $term )) . ')' );
}
$term = $this -> db -> quoteInto ( $cond , $term );
}
if ( $distinct != '' ) {
$distinct = 'DISTINCT ' . $this -> db -> quoteIdentifier ( $distinct );
}
if ( $limit != '' ) {
$limit = implode ( ',' , array_map ( function ( $item ){ return intval ( $item );}, explode ( ',' , $limit )));
}
$result_items = parent :: fetchAll ( 'SELECT ' . (( $distinct ) ? $distinct : implode ( ',' , $select ))
. ' FROM ' . $this -> table ()
. (( $where ) ? ( ' WHERE ' . implode ( ' AND ' , $where )) : '' )
. (( $order ) ? ( $this -> order ( $order , $order [ 'sort' ])) : '' )
. (( $limit ) ? ( ' LIMIT ' . $limit ) : '' ));
$select = $this -> db -> selectExpr ( $select , $distinct );
$where = $this -> db -> whereExpr ( $where );
$order = isset ( $order [ 'sort' ]) ? $this -> order ( $order , $order [ 'sort' ]) : false ;
$limit = $this -> db -> limitExpr ( $limit );
$result_items = parent :: fetchAll (
(( $sql_expression ) ? $sql_expression : ( 'SELECT ' . $select . ' FROM ' . $this -> table ()))
. (( $where ) ? ( ' WHERE ' . $where ) : '' )
. (( $order ) ? ( $order ) : '' )
. (( $limit ) ? ( ' LIMIT ' . $limit ) : '' ),
$sql_expression_params ,
$cache_key
);
return new SqlResultProvider ( $result_items );
}
/**
* @ param $sql_expression null
* @ param $sql_expression_params array
* @ return SqlCriteria
*/
public function criteria ()
public function criteria ( $sql_expression = null , $sql_expression_params = array ())
{
return new SqlCriteria ();
return new SqlCriteria ( $this , $sql_expression , $sql_expression_params );
}
}