EditEntity 方法对记录执行指定的操作,并使该记录可编辑。必须事先已通过调用 GetEntityByDbId 或 GetEntity,或者通过运行查询获取 entity 参数中指定的 Entity 对象。
$session->EditEntity(entity, edit_action_name);
您可以获取 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 方法将指定的值放入特定字段中。如果该字段可更改,那么该方法会为其设置新值(而不会考虑该值是否有效),并返回空字符串。
要确定字段是否包含有效值,请获取该字段的 FieldInfo 对象,然后调用 FieldInfo 对象的 ValidityChangedThisSetValue 方法来验证该字段。如果不能更改该字段,那么返回的字符串将指明为何不能更改该字段。典型的值包括“no such field”、“record is not being edited”和“field is read-only”。 如果字段可以具有多个值而非一个值,请使用 AddFieldValue 方法添加每个新值。使用 SetFieldValue 仍然合法;但对已包含值列表的字段使用 SetFieldValue 将会用单个新值替换整个列表。仅当 Entity 对象可编辑时,才可以调用该方法。
$entity->SetFieldValue(field_name, new_value);
要编辑现有记录,请执行以下步骤:
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);