0

I have been using a controller method post directly to perform some db and social network operations but im finding a few points of failure between it and the hardware — so I came up with the idea of storing all the request in a db table to be used as a queuing system instead so I can process them in my own time rather than real time

The thing I'm struggling with now is handling my requests . I know this isn't very MVC — but its quick fix.

How do I call another controller's method from within my process queue method? I have tried including the file and instantiating it — then passing it the variables i would have done from the web.

function process(){
    $result = $this->mque->get_all();

    include('post.php');      
    $get = new post();


    foreach($result->result_array() as $item){
        $get->index($item['rfid'],$item['station_id'],$item['item']);

    }

}

but i get an error- when i call the normal index method- it runs fine but i get an undefined method error when call it through the instantiated class method- (this is my problem)

Message: Undefined property: post::$db

The why

I am setting the process queue method to run based on a cron job running at a set interval of time.

Originally everything ran to index method of post — but since post::index() can take 10-15 seconds to run and the reader is not multi threaded — someone could use the reader within 7 seconds and the script wouldn't have run completely.

Is there a better way of doing this rather than using my current process method?

update

there is two ways to do this- either use php to fopen/get from the web

or do it sprogramming using $class->method()- i would prefer to do this the first method but dont really see any option with the error i mentioned before

tereško
  • 58,060
  • 25
  • 98
  • 150
Chris Mccabe
  • 1,902
  • 6
  • 30
  • 61

2 Answers2

0

That's easy: you don't have one controller call another. As a rule, if you need something to exist in two different places, you have two options:

  1. Have them both subclass the same object
    • pro: That way the method is already there
    • con: You can only subclass one thing, and you have to build your own class loading system (NOT GOOD)
  2. Have a library (or model) which they both share
    • pro: The method can then be tested better (it is (or it was at one point) easier to unit test models than it is to test controllers), the code can be shared without a custom class-loading syntax.
    • con: This may involve a little refactoring (but it should be as easy as moving the code from the controller's method to a library's method and then simply calling the library in the public controller method).

Either one of those would solve your particular problem. Personally, because of how CI loads controllers, my preference is to create libraries.

cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
  • at this stage i don't have the time to refactor the code, (it is long, its not DRY and does multiple things but its mission critical)- i was looking something quick and dirty to get us going – Chris Mccabe Feb 21 '12 at 11:25
0

CodeIgniter: Load controller within controller

Is this something that could help you out quickly? Check the bottom reply.

Community
  • 1
  • 1
qwertzman
  • 784
  • 1
  • 9
  • 23