Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to reference to database from a CakePHP library
    primarykey
    data
    text
    <p>I've created a library class file in my CakePHP 2.0 app. It's a single PHP class called <code>emailManager</code> Which exists within a folder <code>emailManager</code> within CakePHP's libaray folder. I would love to know what is the simplest way to reference the database from this library class. </p> <p>I would love to be able to do something like <code>$this-&gt;AppModel-&gt;query("SELECT * FROM some_table_in_my_db")</code>, that way I do not have to track DB configurations in separate places, but I'm not sure how to achieve this. </p> <p>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 <code>query</code></p> <p>EDIT: I have constructed a temporary solution, but I know a better one is possible. I have a model in my cake app called <code>MySimpleConstuct</code> and then in the library file I include the <code>MySimpleConstruct</code> Model as followed: </p> <pre><code> // import this model $this-&gt;GivenModel = ClassRegistry::init('MySimpleConstruct'); $this-&gt;GivenModel = new MySimpleConstruct(); // Then it is possible to do as followed: $table_data = $this-&gt;GivenModel-&gt;query('SELECT * FROM like_some_table_dude' WHERE 1); </code></pre> <p>This is not ideal so I still searching for a better solution. Any help would be greatly appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload