Documentation

PrepareFields

CDatabase::PrepareFields(
 string TableName,
 string prefix = "str_",
 string Suffix = ""
)

The method prepares global variables that correspond to table field names to execute writing to the database. Non-static method.

and sets its values All the required type conversion is done automatically.

Creates global variables in the form of ${prefix.field_name.postfix} sets their values to those of global variables with names as per table field names. All the required type conversion is done automatically.

For example, the following will be executed for the int field type:

${prefix.field_name.postfix} = intval(${имя_поля});
For the varchar field type:

${prefix.field_name.postfix} = CDatabase::ForSql(${field_name}, field_size);

Note. The method operates variables of the global scope. Keep this fact in mind when creating main files of components.

Parameters

ParameterDescription
TableName Table name.
prefix Prefix for variables.
Optional; "str_" by default.
Suffix Suffix for variables.
Optional; empty string by default.

See Also

Examples of use

<?
if (strlen($save)>0)
{
    if (CheckFields())
    {
        $DB->PrepareFields("b_form");
        $arFields = array(
            "TIMESTAMP_X"             => $DB->GetNowFunction(),
            "NAME"                    => "'".trim($str_NAME)."'",
            "VARNAME"                 => "'".trim($str_VARNAME)."'",
            "C_SORT"                  => "'".intval($str_C_SORT)."'",
            "FIRST_SITE_ID"           => "'".$DB->ForSql($FIRST_SITE_ID,2)."'",
            "BUTTON"                  => "'".$str_BUTTON."'",
            "DESCRIPTION"             => "'".$str_DESCRIPTION."'",
            "DESCRIPTION_TYPE"        => "'".$str_DESCRIPTION_TYPE."'",
            "SHOW_TEMPLATE"           => "'".trim($str_SHOW_TEMPLATE)."'",
            "MAIL_EVENT_TYPE"         => "'".$DB->ForSql("FORM_FILLING_".$str_VARNAME,50)."'",
            "SHOW_RESULT_TEMPLATE"    => "'".trim($str_SHOW_RESULT_TEMPLATE)."'",
            "PRINT_RESULT_TEMPLATE"   => "'".trim($str_PRINT_RESULT_TEMPLATE)."'",
            "EDIT_RESULT_TEMPLATE"    => "'".trim($str_EDIT_RESULT_TEMPLATE)."'",
            "FILTER_RESULT_TEMPLATE"  => "'".trim($str_FILTER_RESULT_TEMPLATE)."'",
            "TABLE_RESULT_TEMPLATE"   => "'".trim($str_TABLE_RESULT_TEMPLATE)."'",
            "STAT_EVENT1"             => "'".trim($str_STAT_EVENT1)."'",
            "STAT_EVENT2"             => "'".trim($str_STAT_EVENT2)."'",
            "STAT_EVENT3"             => "'".trim($str_STAT_EVENT3)."'"
            );
        if ($ID>0) 
        {
            $DB->Update("b_form", $arFields, "WHERE ID='".$ID."'", $err_mess.__LINE__);
        }
        else 
        {
            $ID = $DB->Insert("b_form", $arFields, $err_mess.__LINE__);
            $new="Y";
        }
        $ID = intval($ID);
        if (strlen($strError)<=0) 
        {
            if (strlen($save)>0) LocalRedirect("form_list.php?lang=".LANGUAGE_ID);
            elseif ($new=="Y") LocalRedirect("form_edit.php?lang=".LANGUAGE_ID."&ID=".$ID);
        }
    }
}
?>


© «Bitrix24», 2001-2024
Up