Reputation: 413
My question is that with example. I have to show all categories of books and with checkboxes in a page which I want to be posted to next view without interacting db and there I want to store them in a hidden field in a comma separated string.
Actually I have a tab for find a book. user clicks on it and it has a list of categories of books listed which I get from categories model and used categories controller index function. I want to post user selected categories to next view like where I will take more info regarding user filtration for books it is coming from another model and controller.
actually I want to know what I have to do I want to develop this find a book function.Should I have a model for this and a controller which they use some table for find a book or can I use any model in any controller for this function.
Upvotes: 1
Views: 2217
Reputation: 1074
Apart from loading the model itself, You can use a model in a controller other than its own as long as there is a relation between the two models. For example, consider the following relation:
"Books" Model BelongsTo "Category" Model
In this scenario, you can use
$this->Book->Category->find() inside BooksController
as well as
$this->Category->Book->find() inside CategoriesController
Coming to your case, it would help if you post your Database Schema. Nevertheless, to find a book, I assume you are using certain parameters like Categories, Genres, Publishers etc. Assuming each parameter will have it's own database table and model, your search hops would be as follows:
Page 1: List of Categories with Check boxes for Selection
Page 2: List of Genres with Check boxes for selection
Page 3: List of Publishers with Check boxes for selection
Page 4: List of Books filtered according to the given parameters
I tried to be as guiding as possible, leave comment if you are still not clear how to go about searching for books. If you need help with passing data from one controller to another, read the CakePHP Manual (http://book.cakephp.org/view/57/Controller-Methods) or Google using "cakephp passing data from one controller to another" as keywords.
Upvotes: 2
Reputation: 2623
Matiasf's suggestion does work, but it is not the recommended way of doing things.
If you add models to the $uses property you'll be loading those models for every action in your controller, which requires more processing power and could have a detrimental effect on your site's performance.
You can read more about why this isn't a good idea in the CakePHP documentation.
The better option is to use the loadModel() controller method.
For example:
BooksController extends AppController {
function index() {
$this->loadModel('Category');
$this->set('categories', $this->Category->find('all'));
}
}
You can find more info about the loadModel method in the documentation: book.cakephp.org/view/977/Controller-Methods#loadModel-992
Upvotes: 3
Reputation: 1118
Regarding the question in the title:
Yes, you can use any model in any controller. Set the
var $uses = array("Model_1", "Model_2", ...);
And the model object should be accessible
$this->Model_1->find(...);
Upvotes: 1