Skip to Content
author's profile photo Former Member
Former Member

MVC: Using m_parent to access model instances

In my main controller I use do_init( ) to create my model instances and hope to access them via me->m_parent and its m_models attribute in my sub_controllers. I want to do this without having to create instances in every sub_controller's do_request, do_handle_event, etc.

DO_REQUEST of sub_controller

data: main_model type ref to cl_bsp_model,

main type ref to cl_bsp_controller2,

test type string.

main ?= me->m_parent.

main_model = main->get_model( 'plmMain' ).


test = main_model->test. "test = attribute in the model.


Everything I need is available is there at runtime however, I can't compile it! I have figured out how to handle this if I create an instance:

data: main type ref to cl_bsp_controller2,

model type ref to zcl_pmfront_m_obj,

det_model type ref to zcl_pmfront_m_det,

table_event type ref to cl_htmlb_event_tableview,

rowselection type i.

main ?= m_parent.


if htmlb_event is bound and htmlb_event->server_event = 'myequnr'.

table_event ?= htmlb_event.

rowselection = table_event->rowselection.

model ?= main->get_model( 'mo' ).

det_model ?= main->get_model( 'md' ).

det_model->sel_event = 'myequnr'.

det_model->sel_equnr = model->zget_selected_equnr(

row = rowselection ).


But this is undesireable because we have to create instances and hardcode the class name into each and every controller method.

I know about storing model instances in an application class, but we shouldn't have to create instances every time we want access to a model in a sub_controller.

I would appreciate any advice on this matter and can provide any additional code if my problem isn't clear. Thanks in advance.

Add a comment
10|10000 characters needed characters exceeded

Related questions

1 Answer

  • Posted on Jul 22, 2005 at 08:46 PM

    >I know about storing model instances in an application ?>class, but we shouldn't have to create instances every >time we want access to a model in a sub_controller.

    You aren't creating a new instance. You are only creating a new reference to the existing instance.

    You have to cast into your more specific instance of the object (application class or model class) in order to access methods or attributes specific to that level of inheritance heirachy. That seems like basic OO to me.

    I usually use the application class to store my model and pass it around to sub-controllers. You still must declare the exact class type of the model and the application class to do this.

    data: model1 type ref to zcl_bsp_m_mfglbl.
      data: appl type ref to zcl_ep_bsp_appl_mfglbl_prt.
    ****Get a Pointer to the application Object
      appl ?= application.
    *  CLEAR appl->message2.
    ****Get a pointer to the Model Object.
      model1 ?= appl->model.

    I supose if you wanted to get wild and crazy you could try using the RTTS to dynamically create your specific class type at runtime. I have never tried that in this context, but this coding might help (it gets the class name).

    ****Trap the popup menu onSelect Event and redirect it our event
      data: class_desc type ref to cl_abap_typedescr.
      class_desc = cl_abap_classdescr=>describe_by_object_ref( me ).
      data: class_name type string.
      class_name = class_desc->get_relative_name( ).

    Even if that would work, I'm not sure how you get around compiler errors at compile time. How does the compiler know that class it should be checking syntax against?!

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member Thomas Jung

      Changing the name of the controller's model class is not a concern for me. It's using a hard-coded class name as the location for my model instances.

      It seems like part of the benefit of MVC is to have reusable MVC objects whose controller can be called from any controller. If you have your super controller/application class name hard-coded, this benefit won't work.

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.