SitePoint Sponsor

User Tag List

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

    Determining Whether to Update or Insert ActiveRecord Data

    Does the below represent a good solution for determining whether something needs to be inserted or updated? I'm trying to make sure this works for string and integers(auto_increment) columns a like. Basing the save type on whether or not the primary key has changed seems like good approach because rarely is it changed. Any problems with this I might be overlooking?

    ActiveRecordSave class method add()
    PHP Code:
    public function add(ActiveRecord $record) {

        
    /*
        * Configuration information about model (table,fields,indexes,ect)
        */
        
    $config ActiveRecordModelConfig::getModelConfig(get_class($record));

        
    /*
        * primary key column name
        */
        
    $primaryKey $config->getPrimaryKey();
        
        
    /*
        * Determine whether to update or insert record
        * 1.) accounts for generated primary keys
        * 2.) accounts for user defined primary keys
        */
        
    if($record->hasProperty($primaryKey)===false || $record->hasChanged($primaryKey)===true) {
        
            
    $this->addInsert($record);
            
        } else {
        
            
    $this->addUpdate($record);
            
        }



  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    The two basic ways to load a object with data from the database are shown below. Both will not result in a change in the objects logged state. Therefore, the hasChanged() method will return false for every property.

    PHP Code:
    $blog = new Blog(90); 
    PHP Code:
    $entity = new ActiveRecordDataEntity();
    $entity->setProperty('id',90);
    $entity->setProperty('title','my title');

    $blog = new Blog($entity); 
    Changes are only logged when set directly through the interface or passed as a array of to the constructor.

    PHP Code:
    $blog = new Blog();
    $blog->id 90
    PHP Code:
    $blog = new Blog(array('id'=>90)); 
    So I believe this type of interface provides a method for both automating the insert of a generated and defined primary key.

    PHP Code:
    $blog = new Blog();
    $blog->title 'whatever';
    $blog->save(); // will be inserted since it lacks a primary key 
    PHP Code:
    $user = new User();
    $user->id 'oddz';
    $user->save(); // will be inserted since its primary key property has changed 
    The only problem occurs when the primary key is changed on accident for a object that represents a live row in the database.

    PHP Code:
    $blog = new Blog(78);
    $blog->id 69
    $blog->save(); // will be inserted because primary key has changed 
    I'm not convinced that is worth being concerned about though.

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    The below represents the current implementation of the add() method. The primary difference between this and the proposed implementation is that hasChanged() only comes into the equation when the primary key is a string. Otherwise the system always updates a record with a primary key and inserts one without (if not a string).

    PHP Code:
        public function add(ActiveRecord $pRecord) {
            
            
    $config ActiveRecordModelConfig::getModelConfig(get_class($pRecord));    
            
    $primaryKey $config->getPrimaryKey();
            
            if(
    $pRecord->hasProperty($primaryKey)===true) {
                
                if(
    is_string($pRecord->getProperty($primaryKey)) && $pRecord->hasChanged($primaryKey)===true) {
                
                    
    $this->addInsert($pRecord);
                
                } else {
                
                    
    $this->addUpdate($pRecord);
                
                }
            
            } else {
            
                
    $this->addInsert($pRecord);
            
            }
        
        } 


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
  •