SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)

    class constants as options or individual methods?

    In regards to the ActiveRecord pattern would it be preferably to use separate methods for returning a object, collection, integer and select object?

    Currently there is one static method find() which a constant can be passed as option which changes how the method works:

    PHP Code:
    ActiveRecord::findAll
    ActiveRecord
    ::findOne
    ActiveRecord
    ::findCount
    ActiveRecord
    ::findSelect 
    The usage then looks something like this:

    counts users returns integer:
    PHP Code:
    User::find(ActiveRecord::findCount); 
    returns collection of users:
    PHP Code:
    User::find(ActiveRecord::findAll); 
    returns first object in collection:
    PHP Code:
    User::find(ActiveRecord::findOne); 
    returns select statement which can be embedded as a subquery:
    PHP Code:
    User::find(ActiveRecord::findSelect); 
    Do think it would be a benefit to also provide a method that abstracts the mode or does it not matter?

    PHP Code:
    User::count();
    User::one();
    User::all();
    User::select(); 
    The reason why I hesitate to just create those methods is because they need to be defined in every model due to < 5.3 support for late static binding. So would it be worth it considering there is a generation script for the model stubs similar to Luke Bakers ActiveRecord?

    thanks

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Example of what a generated model file looks like with the addition of separate methods.

    PHP Code:
    <?php
    class MusicAlbum extends ActiveRecord 

        public static 
    $table 'music_albums';

        public static 
    $fields = array(

            
    'id'
            
    ,'artist'
            
    ,'title'
            
    ,'release_date'

        
    );

        public static 
    $primaryKey 'id';

        public static 
    $dataTypes = array(

            
    'id' => 'int(10) unsigned'
            
    ,'artist' => 'varchar(100)'
            
    ,'title' => 'varchar(255)'
            
    ,'release_date' => 'date'

        
    );

        public static 
    $requiredFields = array(

            
    'artist'
            
    ,'title'
            
    ,'release_date'

        
    );

        public static 
    $defaults = array(

            
    'release_date' => '0000-00-00'

        
    );

        public static function 
    find() {
            
    $args func_get_args();
            return 
    parent::_find(__CLASS__,$args);
        }

        public static function 
    count() {
            
    $args func_get_args();
            
    array_unshift($args,self::findCount);
            return 
    parent::_find(__CLASS__,$args);
        }

        public static function 
    one() {
            
    $args func_get_args();
            
    array_unshift($args,self::findOne);
            return 
    parent::_find(__CLASS__,$args);
        }

        public static function 
    all() {
            
    $args func_get_args();
            
    array_unshift($args,self::findAll);
            return 
    parent::_find(__CLASS__,$args);
        }

        public static function 
    select() {
            
    $args func_get_args();
            
    array_unshift($args,self::findSelect);
            return 
    parent::_find(__CLASS__,$args);
        }

    }
    ?>

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I hate typing long class constant names. The choices seem not so "option" like(if that makes sense), and more a large change in behavior. I say go with the methods.

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    The "modes" only change one or two lines out of 20 or so. They can also be passed using the literal string(s) : 'one','all','count','subquery'. Although providing separate methods isn't really a problem if also appropriate. Having those methods in each class just seems dirty to me. Its something I can get over though. I'd be open to other suggestions, but the nonsupport for late static binding is a killer here. The "modes" are used in an effort to limit code repetition.

    ActiveRecord::_find():
    PHP Code:
        public static function _find($pClassName,$pOptions) {
            
            if(
    self::isConnected()===false) throw new Exception('A database Adaptor has not been set for the '.__CLASS__.' Class.');
        
            
    $model ActiveRecordModelConfig::getModelConfig($pClassName);
            
            
    $mode = !empty($pOptions) && is_array($pOptions[0])===false?array_shift($pOptions):self::findAll;
            
            
    $node = new ActiveRecordSelectNode($model,new ActiveRecordFindConfig(!empty($pOptions)?$pOptions[0]:array()));
            
    $select strcasecmp($mode,self::findCount)==0?new ActiveRecordCount($node,$pOptions):new ActiveRecordSelect($node,$pOptions);
            
            if(
    strcmp($mode,self::findSelect)==0) return $select// return without reseting count
            
            //echo '<p>',$select->toSql(),'</p>';
            //echo '<pre>',print_r($select->getBindData()),'</pre>';
            //return;
            
            
    $stmt $select->query(self::$_db);
            
    ActiveRecordSelectNode::resetCount();
            
    $collectionAgent = new ActiveRecordCollectionAgent($select);
            
            if(
    strcmp($mode,self::findCount)==0) { return $stmt->fetchColumn(); }

            while(
    $row $stmt->fetch(PDO::FETCH_ASSOC)) {
                
    $collectionAgent->process($row,$node);
            }

            
    $records $collectionAgent->getRecords();
            
            return 
    strcmp($mode,self::findOne)==0?count($records)!=0?$records[0]:null:$records;
            
        } 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •