TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
Best way to pass an ORM object between controllers?
  • Hi, if anyone could please advise me on the below I would appreciate it - I'm new to Kohana and am open to suggestions.

    I have Model_Group that extends ORM.

    I have Controller_Group that gets a new ORM:

    public function before()
    {
         global $orm_group;
         $orm_group = ORM::factory('Group');
    }
    

    ...and it has various methods that use it to get different subsets of data, such as...

    public function action_get_by_type()
    {
        global $orm_group;
        $type = $this->request->param('type');
        $result = $orm_group->where('type', '=', $type)->find_all();
    }
    

    Then I have another controller (in a separate module) that I want to use to manipulate the object and call the relevant view. Let's call it Controller_Pages.

    $orm_object = // Get the $result from Controller_Group somehow!
    $this->template->content = View::factory( 'page1' )
        ->set('orm_object', $orm_object);
    

    What is the best way to pass the ORM object from Controller_Group to Controller_Pages? I've looked at using Request/Response but I've got a feeling that's not the right way (and couldn't get it to work), so I'm thinking it would be better called via a static function. Is this a good idea? If not, why not, and what better way is there of doing it?

    The reason for separating them out into different controllers is because I want to be able to re-use the methods in Controller_Group from other modules. Each module may want to deal with the object in a different way.

    Many thanks, Steve

  • How are you even going from Controller_Group to Controller_Pages?

    Maybe you should create a base controller and create a method there ...

     Controller_Base 
     {
    
         public $orm_group;
    
         ...
    
     }
    
     Controller_Group extends Controller_Base 
     {
    
          public function action_something() 
          {
              $this->orm_group =  ...;
          }
    
     }
    
     Controller_Pages extends Controller_Base {
    
          public function action_something() 
          {
              $orm_group = $this->orm_group;
          }
    
     }
    

    But if you are doing passing control of the request from 2 controllers, show us your code that does that ... does it look like this? http://forum.kohanaframework.org/discussion/comment/40451

    Maybe you do this

    Request::factory('pages/action')
                ->bind('orm_group', $orm_group);
    
  • Hi chrisgo, thank you for your reply. I was being somewhat deliberately ambiguous in my first post, as I have tried a few things either without success, or without feeling that it was "the Kohana way". It's important to note that currently these two Controllers are in different modules. The routes for these modules are set up correctly.

    Controller_Group is in module "abc" Controller_Pages is in module "xyz"

    Here's what I've tried:

    1) Using Request::factory() from Controller_Pages...

    // Controller_Pages (module xyz)
    public function action_view()
    {
        $orm_object = Request::factory( 'abc/group/get_by_type/'.$type )->execute();
        $this->template->content = View::factory( 'page1' )
            ->set('orm_object', $orm_object);
    }
    
    // Controller_Group (module abc)
    public function action_get_by_type()
    {
        global $orm_group;
        $type = $this->request->param('type');
        $result = $orm_group->where('type', '=', $type)->find_all();
        // Then I tried all sorts to "return" $result but with no luck - I think probably because I just haven't grasped the concept of Request:: properly. I am using v3.3.1 so the whole request/response syntax has changed and I couldn't find any consistent guides on how to do it - everything seemed to say return it in the "body" property but this would only accept strings.
    }
    

    2) Using static method:

    // Controller_Pages (module xyz)
    public function action_view()
    {
        $orm_object = Controller_Group::action_get_by_type($type);
        $this->template->content = View::factory( 'page1' )
            ->set('orm_object', $orm_object);
    }
    
    // Controller_Group (module abc)
    public function action_get_by_type($type)
    {
        global $orm_group;
        $result = $orm_group->where('type', '=', $type)->find_all();
        return $result;
    }
    
    // This works, but I felt that it probably wasn't "the Kohana way" of doing things.
    

    It might be worth mentioning that I am converting my whole application over from CodeIgniter 2 to Kohana 3.3.1, so I may be stuck in my old ways of doing things and just not seeing the light.

    Your suggestion of having a "Base" controller is interesting. I assume the base Controller could be in the abc module, but then all other modules extend it? This isn't really an issue as the other modules would never be there without module abc, but it does go against everything I've learnt about modularity and how the inter-dependencies should always be via some sort of interface (as opposed to being "included").

    Like I said I am totally open to suggestions. I realise there are a few ways of doing this, but I just don't know which is the best/proper one because I'm too new to Kohana. If the proper way is option 1 above, I'd really appreciate if someone could clarify how you're supposed to "return" anything other than a string using $this->response();

    Your time is very much apprecaited as I have now been trying to do this for over two days, and feel like I'm getting nowhere :(

  • Someone on Stack Overflow has just suggested putting the method get_by_type inside the ORM model so that it can be called from anywhere, which is another option! I'm so confused!

  • Decided to put the method inside the ORM model, as described on here: http://stackoverflow.com/questions/19683486/kohana-best-way-to-pass-an-orm-object-between-controllers

    Hope this helps someone else out in the future.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion