1

I've created a library class file in my CakePHP 2.0 app. It's a single PHP class called emailManager Which exists within a folder emailManager within CakePHP's libaray folder. I would love to know what is the simplest way to reference the database from this library class.

I would love to be able to do something like $this->AppModel->query("SELECT * FROM some_table_in_my_db"), that way I do not have to track DB configurations in separate places, but I'm not sure how to achieve this.

Also, I feel it is important to mention that the tables I am working with do not adhere to CakePHP table naming convention. They predate our use of CakePHP and so I cannot change my tables to fit CakePHP's model format. this is why I want generic database access via something like query

EDIT: I have constructed a temporary solution, but I know a better one is possible. I have a model in my cake app called MySimpleConstuct and then in the library file I include the MySimpleConstruct Model as followed:

 // import this model
 $this->GivenModel = ClassRegistry::init('MySimpleConstruct');
 $this->GivenModel = new MySimpleConstruct();

 // Then it is possible to do as followed:
 $table_data = $this->GivenModel->query('SELECT * FROM like_some_table_dude' WHERE 1);

This is not ideal so I still searching for a better solution. Any help would be greatly appreciated.

AD7six
  • 63,116
  • 12
  • 91
  • 123
usumoio
  • 3,500
  • 6
  • 31
  • 57
  • Duplicate of ? – azBrian Jul 01 '13 at 20:13
  • @azBrian I don't think it is, the questions are related, but that was one of the questions I reviewed while preparing this one. Would you have any advice on how to construct a better solution than the one my edit here describes? I'm still very interested in a better answer than the one I'm using. – usumoio Jul 01 '13 at 20:24

1 Answers1

1

@John Galt, I suppose it's not an exact duplicate but it is very similar and the resolution does appear to apply to your situation very directly. The other technique you could consider using would be to instantiate the Library in the controller and than give it a reference of the model.

class TestController extends AppController {
    function index(){
        App::uses('TheLibrary', 'Lib');
        $obj = new TheLibrary();
        $obj->GivenModel = &$this->GivenModel;
    }
}

-EDIT- And then within the library you've written do something like this.

class TheLibrary {
    var $GivenModel = null;

    function some_query(){
        return $this->GivenModel->query('SELECT * FROM like_some_table_dude WHERE 1');
    }
}

The first code snippet is of the Controller instantiating your library and then giving the library a reference to the Model as the property GivenModel. The "&" symbol makes the assignment a reference (see How does the '&' symbol in PHP affect the outcome?). The second code snippet is of a sample of how the library would use that property.

I do understand that you are trying to use a model from the library and that is what the solution you have in your edit and my proposed solution both do. However I will note again that this is not proper MVC convention and you should reconsider how you are using Libraries.

Community
  • 1
  • 1
azBrian
  • 661
  • 1
  • 5
  • 19
  • Ah okay I now think I see where the confusion is coming from. I did not explain myself well enough. I'm looking for a way to reference the database of the main app from within the library. I think I have given the impression that I am looking for a way to reference the library from a model in the app which is not what I'm having trouble working on. Thank you though. I'll be checking out your `&` notation later. – usumoio Jul 01 '13 at 22:51
  • Hmmm the edit to your question is interesting. I will review this after the long work day. Cheers dude. – usumoio Jul 03 '13 at 14:11