I'm wondering where my custom database layer/classes should go in Cake's app folder.
- Should I create them as components and store them in the components folder?
- Should I create them as classes and put them in a class folder? If so, how would those be instantiated in my controller?
This means that all my controllers will have the following line:
var $uses = array('');
Edit: Basically, I'll be writing my queries by hand and will be calling my classes inside the controller. I'm wondering about the optimal way to implement this. I do realize that this involves tossing out the Model approach in CakePHP.
Edit2: I've tried using custom queries in my model - ie. Model->query('sql here...'). Howeve开发者_如何学Gor, even though you can specify parameters to the query, I think they are not prepared/escaped in a fashion that protects you from sql injection?
Database layer is linked to Model not Controller (or Component). Cake offers a way to implement that through Datasources: http://book.cakephp.org/view/1075/DataSources
Based on your comments, it sounds like you just want to write your own queries. You can do this with Model::query( SQL_STRING ). In your controller,
function someaction() {
$result = $this->Mymodel->query( 'SELECT name FROM users WHERE id=5' );
}
Your $result array will look a little weird, so I recommend using the CakePHP Set functionality to morph them into what you need. http://book.cakephp.org/view/640/Set
精彩评论