xo::db::CrClass instproc fetch_object (public)
<instance of xo::db::CrClass> fetch_object -item_id item_id \ [ -revision_id revision_id ] -object object \ [ -initialize on|off ]
Defined in /var/www/openacs.org/packages/xotcl-core/tcl/cr-procs.tcl
Load a content item into the specified object. If revision_id is provided, the specified revision is returned, otherwise the live revision of the item_id. If the object does not exist, we create it.
- Switches:
- -item_id (required)
- -revision_id (optional, defaults to
"0"
)- -object (required)
- -initialize (optional, boolean, defaults to
"true"
)- Returns:
- cr item object
- Testcases:
- xotcl_core_tutorial_4, xowiki_test_cases
Source code: # :log "-- generic fetch_object [self args]" if {![nsf::is object $object]} { # if the object does not yet exist, we have to create it :create $object } set raw_atts [::xo::db::CrClass set common_query_atts] #:log "-- raw_atts = '$raw_atts'" set atts [list] foreach v $raw_atts { switch -glob -- $v { publish_status {set fq i.$v} storage_type {set fq i.$v} creation_date {set fq o.$v} creation_user {set fq o.$v} package_id {set fq o.$v} default {set fq n.$v} } lappend atts $fq } foreach {slot_name slot} [array get :db_slot] { switch -glob -- $slot { ::xo::db::CrItem::slot::text { # # We need the rule, since insert the handling of the sql # attribute "text" is somewhat magic. On insert, one can use # the automatic view with column_name "text, on queries, one # has to use "data". Therefore, we cannot use simply # -column_name for the slot. # lappend atts "n.data AS text" } ::xowiki::Page::slot::text { # # This is just a hotfix for now. # #ns_log notice [$slot serialize] lappend atts "n.data as text" } ::xo::db::CrItem::slot::name { lappend atts i.[$slot column_name] } ::xo::db::Object::slot::context_id { # # If we are fetching by revision_id, skip the context_id, # since on object-save-operations, we want to keep the # context_id of the item, and not the context_id from the # revision. # if {$revision_id == 0} { # # Fetch by item_id. # lappend atts o.[$slot column_name] } } ::xo::db::Object::slot::* { lappend atts o.[$slot column_name] } default { lappend atts n.[$slot column_name] } } } if {$revision_id} { $object set revision_id $revision_id set sql [subst { select [join $atts ,], i.parent_id from ${:table_name}i n, cr_items i, acs_objects o where n.revision_id = :revision_id and i.item_id = n.item_id and o.object_id = n.revision_id }] set selection [lindex [::xo::dc sets -prepare integer fetch_object_from_revision_id $sql] 0] $object mset [ns_set array $selection] } else { # # We fetch the creation_user and the modifying_user by returning # the creation_user of the automatic view as modifying_user. In # case of troubles, comment next line out. # lappend atts "n.creation_user as modifying_user" $object set item_id $item_id $object db_1row [:qn fetch_from_view_item_id] " select [join $atts ,], i.parent_id from ${:table_name}i n, cr_items i, acs_objects o where i.item_id = :item_id and n.${:id_column} = coalesce(i.live_revision, i.latest_revision) and o.object_id = i.item_id" } # # The method "db_1row" treats all newly created variables as # instance variables, so we can see vars like "__db_sql", # "__db_lst" that we do not want to keep. # foreach v [$object info vars __db_*] { $object unset $v } # # Deactivate compatibility with versions before OpenACS 5.2 # (2005), since this is a busy code, but leave it here for easy # reactivating in legacy applications. # #if {[apm_version_names_compare [ad_acs_version] 5.2] <= -1} { # set parent_id [$object set parent_id] # $object set package_id [::xo::dc get_value get_pid { # select package_id from cr_folders where folder_id = :parent_id # } #} # :log "--AFTER FETCH\n[$object serialize]" if {$initialize} {$object initialize_loaded_object} return $objectXQL Not present: Generic, PostgreSQL, Oracle