DooPHP IRC channel


DooActiveRecord Suggesstion

Discussion about new desired features.

DooActiveRecord Suggesstion

Postby maxpert » Wed Jan 06, 2010 3:20 pm

I really love the codeigniter's modified active record for accessing database; due to two reasons. It's simple and quick to learn for anybody who has been writing queries; gives feel of more power to bend query creation rather than creating class and associating everything. I have even once made my own active record query creation class. I will love to have it parallel with existing DooModel (correct me if I am wrong or there is already such neat system)
maxpert
 
Posts: 10
Joined: Wed Jan 06, 2010 2:27 pm

Re: DooActiveRecord Suggesstion

Postby ximian » Wed Jan 06, 2010 3:45 pm

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
ximian
 
Posts: 143
Joined: Wed Nov 25, 2009 1:51 am
Location: Portugal

Re: DooActiveRecord Suggesstion

Postby maxpert » Wed Jan 06, 2010 3:57 pm

Yeah I will need some serious opinion on that... I mean as I mentioned before I can really quickly integrate PhARoS into existing system without any cost but still; I will love to have sexy style Doo's own active-record.

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
maxpert
 
Posts: 10
Joined: Wed Jan 06, 2010 2:27 pm


Return to Features Request

Who is online

Users browsing this forum: No registered users and 1 guest

cron