Changeset 8166
- Timestamp:
- 03/31/08 09:18:21 (7 months ago)
- Files:
-
- plugins/sfPropelVersionableBehaviorPlugin/trunk/README (modified) (1 diff)
- plugins/sfPropelVersionableBehaviorPlugin/trunk/lib/model/ResourceVersion.php (modified) (3 diffs)
- plugins/sfPropelVersionableBehaviorPlugin/trunk/lib/sfPropelVersionableBehavior.class.php (modified) (4 diffs)
- plugins/sfPropelVersionableBehaviorPlugin/trunk/test/unit/PropelVersionableBehaviorTest.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfPropelVersionableBehaviorPlugin/trunk/README
r8164 r8166 441 441 == Changelog == 442 442 443 === 2008-03-30 | Trunk === 444 443 === 2008-03-31 | Trunk === 444 445 * francois: Made incremental storage rely on a real version comparison, rather than the array of modified columns. Fixes modified columns not being saved when using `toVersion`. 445 446 * francois: Added the ability to declare related objects to save at behavior declaration 446 447 * francois: Fixed `ResourceVersion::getResourceInstance()` creates new objects and saving these objects creates a new row in the resource table (#3229) plugins/sfPropelVersionableBehaviorPlugin/trunk/lib/model/ResourceVersion.php
r8159 r8166 24 24 * @param Array $withObjects Optional list of object classes to create and attach to the current resource 25 25 */ 26 public function populateFromObject(BaseObject $resource, $withObjects = array(), $withVersion = true , $modifiedColumns = array())26 public function populateFromObject(BaseObject $resource, $withObjects = array(), $withVersion = true) 27 27 { 28 28 if($resource->isNew()) … … 34 34 if($withVersion) 35 35 { 36 $this->setNumber($resource->getVersion()); 36 $version = $resource->getVersion(); 37 $this->setNumber($version); 38 if($previousResourceVersion = $resource->getResourceVersion($version - 1)) 39 { 40 $previousAttributes = $previousResourceVersion->getAttributesArray(); 41 } 37 42 } 38 39 $previousResourceVersion = $withVersion ? $resource->getResourceVersion($resource->getVersion() - 1) : null; 40 $previousResourceAttributeVersions = array(); 43 else 44 { 45 $previousResourceVersion = null; 46 } 41 47 foreach ($resource->getPeer()->getFieldNames() as $attribute_name) 42 48 { 43 // For each attribute, we either create a new AttributeVersion, or reference an older one if not modified 44 if(!$previousResourceVersion || in_array($attribute_name, $modifiedColumns)) 49 // For each attribute, we either create a new AttributeVersion, 50 // or reference an older one if not modified 51 $getter = sprintf('get%s', $attribute_name); 52 if($previousResourceVersion && $resource->$getter() == $previousAttributes[$attribute_name]['value']) 53 { 54 // Attribute not modified 55 // So we use the attribute from a previous version 56 $attributeVersionId = $previousAttributes[$attribute_name]['id']; 57 $isModified = false; 58 } 59 else 45 60 { 46 61 // First version or modified attribute 47 $getter = sprintf('get%s', $attribute_name);48 62 $attributeVersion = new ResourceAttributeVersion(); 49 63 $attributeVersion->setAttributeName($attribute_name); 50 64 $attributeVersion->setAttributeValue($resource->$getter()); 51 65 $attributeVersion->save(); 52 53 66 $attributeVersionId = $attributeVersion->getId(); 54 67 $isModified = true; 55 }56 else57 {58 // Attribute not modified59 // So we use the attribute from a previous version60 if(!$previousResourceAttributeVersions)61 {62 foreach($previousResourceVersion->getResourceAttributeVersions() as $resourceAttributeVersion)63 {64 $previousResourceAttributeVersions[$resourceAttributeVersion->getAttributeName()] = $resourceAttributeVersion->getId();65 }66 }67 68 $attributeVersionId = $previousResourceAttributeVersions[$attribute_name];69 $isModified = false;70 68 } 71 69 … … 115 113 return ResourceAttributeVersionPeer::doSelect($c); 116 114 } 115 116 public function getAttributesArray() 117 { 118 $ret = array(); 119 foreach($this->getResourceAttributeVersions() as $attribute) 120 { 121 $ret[$attribute->getAttributeName()] = array( 122 'value' => $attribute->getAttributeValue(), 123 'id' => $attribute->getId() 124 ); 125 } 126 127 return $ret; 128 } 117 129 130 public function getAttributeValue($name) 131 { 132 $c = new Criteria(); 133 $c->add(ResourceAttributeVersionHashPeer::RESOURCE_VERSION_ID, $this->getId()); 134 $c->addJoin(ResourceAttributeVersionHashPeer::RESOURCE_ATTRIBUTE_VERSION_ID, ResourceAttributeVersionPeer::ID); 135 $c->add(ResourceAttributeVersionPeer::ATTRIBUTE_NAME, $name); 136 $attribute = ResourceAttributeVersionPeer::doSelectOne($c); 137 138 return $attribute->getAttributeValue(); 139 } 140 118 141 public function getModifiedResourceAttributeVersions() 119 142 { plugins/sfPropelVersionableBehaviorPlugin/trunk/lib/sfPropelVersionableBehavior.class.php
r8164 r8166 199 199 * @param array Optional list of related object classes to version together with the main object 200 200 */ 201 public function addVersion(BaseObject $resource, $createdBy = '', $comment = '', $withObjects = array())201 public function addVersion(BaseObject $resource, $createdBy = '', $comment = '', $withObjects = null) 202 202 { 203 203 if (self::versionConditionMet($resource)) … … 219 219 // resourceVersion will be saved in the postSave() method, when the resource has primary and foreign key determined 220 220 221 if( $withObjects != self::getBehaviorParameter(get_class($resource), 'with', array()))221 if(is_array($withObjects) && $withObjects != self::getBehaviorParameter(get_class($resource), 'with', array())) 222 222 { 223 223 $resource->versionWithObjects = $withObjects; … … 339 339 self::incrementVersion($resource); 340 340 } 341 // modified columns array will be reset by the save(), but we need it in the postSave()...342 // So we save it for later.343 $modifiedColumns = array();344 foreach ($resource->getPeer()->getFieldNames() as $attribute_name)345 {346 $attribute_colname = $resource->getPeer()->translateFieldName($attribute_name, BasePeer::TYPE_PHPNAME, BasePeer::TYPE_COLNAME);347 if($resource->isColumnModified($attribute_colname))348 {349 $modifiedColumns[] = $attribute_name;350 }351 }352 $resource->versionModifiedColumns = $modifiedColumns;353 341 } 354 342 … … 380 368 $withObjects = isset($resource->versionWithObjects) ? $resource->versionWithObjects : self::getBehaviorParameter(get_class($resource), 'with', array()); 381 369 $resource->resourceVersion->setResourceId($resource->getPrimaryKey()); 382 $resource->resourceVersion->populateFromObject($resource, $withObjects, true , $resource->versionModifiedColumns);370 $resource->resourceVersion->populateFromObject($resource, $withObjects, true); 383 371 $resource->resourceVersion->save(); 384 372 $resource->resourceVersion = null; 385 373 } 386 unset($resource->versionModifiedColumns);387 374 unset($resource->wasModified); 388 375 } plugins/sfPropelVersionableBehaviorPlugin/trunk/test/unit/PropelVersionableBehaviorTest.php
r8159 r8166 402 402 sfConfig::set('app_sfPropelVersionableBehaviorPlugin_auto_versioning', true); 403 403 404 // #1563 sfPropelVersionableBehaviorPlugindoes not create a version if YourClass::versionConditionMet() is not found405 $t->diag('#1563 : sfPropelVersionableBehaviorPlugin does not create a version if YourClass::versionConditionMet() is not found');404 // #1563 does not create a version if YourClass::versionConditionMet() is not found 405 $t->diag('#1563: sfPropelVersionableBehaviorPlugin does not create a version if YourClass::versionConditionMet() is not found'); 406 406 407 407 $r = _create_resource(); … … 414 414 415 415 // #1564 crashes while creating a new version if no prior version exists 416 $t->diag('#1564 : sfPropelVersionableBehaviorPlugin crashes while creating a new version if no prior version exists');416 $t->diag('#1564: sfPropelVersionableBehaviorPlugin crashes while creating a new version if no prior version exists'); 417 417 418 418 $r = _create_resource(); … … 427 427 } 428 428 429 #3229 Plugin creates new objects when restoring a resource from a resource version 429 // #3229 creates new objects when restoring a resource from a resource version 430 $t->diag('#3229: sfPropelVersionableBehaviorPlugin creates new objects when restoring a resource from a resource version'); 430 431 $r = _create_resource(); 431 432 $r->setByName($test_class_title_column, 'v1', BasePeer::TYPE_FIELDNAME);