< 上一课 | 下一课 >

修改记录

要修改记录的内容,请首先通过调用 Session 对象的 GetEntity 方法来检索记录,然后再调用其 EditEntity 方法。

EditEntity 方法对记录执行指定的操作,并使该记录可编辑。必须事先已通过调用 GetEntityByDbIdGetEntity,或者通过运行查询获取 entity 参数中指定的 Entity 对象。

EditEntity 的语法如下:
$session->EditEntity(entity, edit_action_name); 
  • session - 一个 Session 对象,表示当前的数据库访问会话。
  • entity - Entity 对象,对应于要编辑的记录。
  • edit_action_name - 一个字符串,包含要启动的编辑操作的名称(例如,modifyresolve)。

    您可以获取 edit_action_name 参数的合法值的列表,调用相应 EntityDef 对象的 GetActionDefNames 方法。

例如:
# Build Session object...

# Edit the record whose ID is "BUGDB00000010" using the "modify" action 
$objtoedit = $sessionobj->GetEntity("defect", "BUGDB00000010"); 
$sessionobj->EditEntity($objtoedit,"modify"); 

调用 EditEntity 方法之后,您可以调用 Entity 对象的方法来修改对应记录的字段。您还可以获取有关字段中数据类型或作为整体的记录的更多信息,或可以更改当前操作期间字段的行为。

以下是在编辑记录和字段值时可使用的一些方法:
  • 修改字段值:SetFieldValue、AddFieldValue 和 DeleteFieldValue
  • 添加/删除附件:AddAttachmentFieldValue 和 DeleteAttachmentFieldValue
  • 获取字段组的状态:GetInvalidFieldValues 和 GetFieldsUpdatedThisSetValue/Group/Action
  • 从其 FieldInfo 中获取个别字段的状态:GetValidationStatus 和 GetMessageText

SetFieldValue 方法将指定的值放入特定字段中。如果该字段可更改,那么该方法会为其设置新值(而不会考虑该值是否有效),并返回空字符串。

要确定字段是否包含有效值,请获取该字段的 FieldInfo 对象,然后调用 FieldInfo 对象的 ValidityChangedThisSetValue 方法来验证该字段。如果不能更改该字段,那么返回的字符串将指明为何不能更改该字段。典型的值包括“no such field”、“record is not being edited”和“field is read-only”。 如果字段可以具有多个值而非一个值,请使用 AddFieldValue 方法添加每个新值。使用 SetFieldValue 仍然合法;但对已包含值列表的字段使用 SetFieldValue 将会用单个新值替换整个列表。仅当 Entity 对象可编辑时,才可以调用该方法。

SetFieldValue 的语法为:
$entity->SetFieldValue(field_name, new_value); 
  • entity - 一个 Entity 对象,表示用户数据库记录。
  • field_name - 一个字符串,包含该 Entity 对象的有效字段名。
  • new_value - 一个字符串,包含新值。
如果允许对该字段进行更改,那么此方法将返回空字符串;否则,该方法将返回包含错误说明的字符串。

要编辑现有记录,请执行以下步骤:

  1. 使用 Session 对象的方法,获取要编辑的 Entity 对象。您可以使用 Session 对象的方法,通过查询来查找与所定义的条件匹配的记录,然后处理查询结果集中的记录。
  2. 要使现有的 Entity 对象可编辑,请调用 Session 对象的 EditEntity 方法。
  3. 使用 Entity 对象的方法修改记录中的数据。
  4. 完成编辑记录后,分别调用 Entity 对象的 ValidateCommit 方法来验证该记录,并将您的更改落实到数据库中。

示例

以下示例列出 Rational ClearQuest 用户数据库中的所有缺陷记录,并修改其中一条记录。 该程序:
  • 列出 SAMPL 数据库中的所有缺陷记录,并选择“id”、“Headline”和“State”作为显示字段。
  • 将缺陷记录 SAMPL00000012 的“Description”字段修改为“This defect has been modified.”。
use CQPerlExt; 
#Getting the session 
my $session = CQSession::Build(); 
CQSession::UserLogon ($session, "admin", "", "SAMPL", "Lab3"); 

my $querydef = $session->BuildQuery ("defect"); 
$querydef->BuildField ("id"); 
$querydef->BuildField ("headline"); 
my $resultset = $session->BuildResultSet ($querydef); 
$resultset->Execute(); 
while (($resultset->MoveNext()) == 1) { 
   $id = $resultset->GetColumnValue(1); 
   $rec = $session->GetEntity("Defect", $id); 
   $head = $rec->GetFieldValue("Headline")->GetValue(); 
   $state= $rec->GetFieldValue("State")->GetValue(); 
   print "$id, $head, $state. \n"; 
      if ($id eq "SAMPL00000012") { 
         $session->EditEntity($rec, "Modify"); 
         $rec->SetFieldValue("description", "This defect has been modified."); 
         $status = $rec->Validate();
         if ( $status ){
            $rec->Revert;
            die "Validation Error: $status \n"
          } else {
               # Only commit the changes if the validation is first successful. 
               $rec->Commit(); 
             }
      }
 } 
CQSession::Unbuild($session); 
< 上一课 | 下一课 >

反馈