0

I've been using hooks for my database migrations in my CI app using the post_controller_constructor hook.

In my recent revisions, I've changed my main Model variables from being set within the class constructor to being loaded from a db table. When I attempt to execute my new migration with the DB Forge data in it, my model doesn't load because it gets hung up on the fact that the table doesn't exist yet.

So I obviously can't make any database calls using pre_controller because I have no access to the main CI object. post_controller_constructor seems to execute after my Model constructor is loaded. What can I do to grab from the database before my Model is loaded?

Jared Eitnier
  • 7,012
  • 12
  • 68
  • 123

1 Answers1

1

Could you do it in the actual constructor (before calling parent::__construct())? I don't see the need for hooks in this case.

landons
  • 9,502
  • 3
  • 33
  • 46
  • I was thinking about just going this route. Hooks seemed to be a nice clean approach but I don't it was designed for what I want to do. – Jared Eitnier May 28 '13 at 18:30
  • Extending the `CI_Model` class is probably the better approach in this case. – landons May 28 '13 at 18:36
  • Where at, in my hook class? – Jared Eitnier May 28 '13 at 18:37
  • Create a new file: application/core/MY_Model.php. Define a class `MY_Model`, which extends `CI_Model`. Do your migration checks in the constructor (before calling `parent::__construct()`) – landons May 28 '13 at 18:38
  • Well, part of my issue was that I was auto-loading my main Model, which apparently loads prior to the `post_controller_constructor` hook. Either way, I decided to go with this approach http://stackoverflow.com/a/11744750/1133306. Thanks for your input though. – Jared Eitnier May 28 '13 at 19:27
  • 1
    Glad you figured it out. CI hooks feel a little underdeveloped if you ask me. – landons May 28 '13 at 20:11
  • Agreed - they could at least expand on the docs a bit. Currently it's very obscure. – Jared Eitnier May 28 '13 at 20:27