DooPHP IRC channel
class xxx extends DooModel{
function __construct(){
$this->_opts = array();
...
}
// prepare where
function & pWhere( $key, $value='', $isStatement = false, $and = true ){
if( in_array( $key, $this->_fields ) ){
$this->$key = $isStatement ? new DooDbExpression ($value) : $value;
}else{
$this->_opts[ 'where' ] .= ( empty( $this->_opts[ 'where' ] ) ? '(' : ( $and ? 'AND(' : 'OR(' ) ) . $key . ( empty( $value ) ? '' : ( $isStatement ? new DooDbExpression( $value ) : $this->db()->quote( $value ) ) ) . ')';
}
return $this;
}
// reset model variables
function resetAll(){
// unset all fields
foreach( $this->_fields as $f ){
if( isset( $this->$f ) )
unset( $this->$f );
}
// init options
$this->_opts = array();
}
// prepare limit
public function pLimit($limit=1 ){
$this->_opts['limit'] = $limit;
}
// prepare order
public function pOrderby($column, $order=true){
if( $order === true ){
$o = ' ASC';
}elseif( $order === false ){
$o = ' DESC';
}elseif( $order === 'random' ){
$o = 'random()';
}else{
$o = '';
}
$this->_opts['custom'] = 'order by ' . $column . $o;
}
// quote: used eg on where's, $this->pWhere( '... =' . $this->quote( ... ));
function quote( $str ){
return $this->db()->quote( $str );
}
// asArray methods
public function relateAsArray( $rmodel, $opt=array() ){
$this->_opts['asArray'] = true;
return $this->relate($rmodel,array_merge( $this->_opts, $opt ) );
}
public function find($opt=array()){
$this->_opts['asArray'] = false;
return parent::find( array_merge( $this->_opts, $opt ) );
}
public function getOne($options=array()){
$this->_opts['asArray'] = false;
return parent::getOne(array_merge( $this->_opts, $options ));
}
public function getOneAsArray($options=array()){
$this->_opts['asArray'] = true;
return parent::getOne(array_merge( $this->_opts, $options ));
}
public function findAsArray($opt=array()){
$this->_opts['asArray'] = true;
return parent::find( array_merge( $this->_opts, $opt ) );
}
}ximian wrote:Hi,
I really like the active record pattern. One of the problems i see in dooModel is that we must define all settings in the same insert/update/delete call (by other hand, with a active record your code become much much readable) because we cannot prepare queries.
I have some doomodel methods that simply simulates prepare queries (my p's, pWhere, pLimit, ..):
- Code: Select all
class xxx extends DooModel{
function __construct(){
$this->_opts = array();
...
}
// prepare where
function & pWhere( $key, $value='', $isStatement = false, $and = true ){
if( in_array( $key, $this->_fields ) ){
$this->$key = $isStatement ? new DooDbExpression ($value) : $value;
}else{
$this->_opts[ 'where' ] .= ( empty( $this->_opts[ 'where' ] ) ? '(' : ( $and ? 'AND(' : 'OR(' ) ) . $key . ( empty( $value ) ? '' : ( $isStatement ? new DooDbExpression( $value ) : $this->db()->quote( $value ) ) ) . ')';
}
return $this;
}
// reset model variables
function resetAll(){
// unset all fields
foreach( $this->_fields as $f ){
if( isset( $this->$f ) )
unset( $this->$f );
}
// init options
$this->_opts = array();
}
// prepare limit
public function pLimit($limit=1 ){
$this->_opts['limit'] = $limit;
}
// prepare order
public function pOrderby($column, $order=true){
if( $order === true ){
$o = ' ASC';
}elseif( $order === false ){
$o = ' DESC';
}elseif( $order === 'random' ){
$o = 'random()';
}else{
$o = '';
}
$this->_opts['custom'] = 'order by ' . $column . $o;
}
// quote: used eg on where's, $this->pWhere( '... =' . $this->quote( ... ));
function quote( $str ){
return $this->db()->quote( $str );
}
// asArray methods
public function relateAsArray( $rmodel, $opt=array() ){
$this->_opts['asArray'] = true;
return $this->relate($rmodel,array_merge( $this->_opts, $opt ) );
}
public function find($opt=array()){
$this->_opts['asArray'] = false;
return parent::find( array_merge( $this->_opts, $opt ) );
}
public function getOne($options=array()){
$this->_opts['asArray'] = false;
return parent::getOne(array_merge( $this->_opts, $options ));
}
public function getOneAsArray($options=array()){
$this->_opts['asArray'] = true;
return parent::getOne(array_merge( $this->_opts, $options ));
}
public function findAsArray($opt=array()){
$this->_opts['asArray'] = true;
return parent::find( array_merge( $this->_opts, $opt ) );
}
}
What do you think?
Francisco A
Users browsing this forum: No registered users and 1 guest