0

In my application, i have many methods that my controllers use commonly. At first i copied them to controllers, then i found out that i must put them in AppController. So i. reach those methods from derived controllers by "$this->commonmethod"

The methods that i put into AppController creates different types of data, so i need to put them to 4-5 different tables in my database. Most of the tables don't have a relation between each other.

But most of my controllers will use that tables to fetch related data for them.

(I checked the examples in cookbook, there are examples about Blogging, category and tags. Where tables have relation between them)

  1. Should i put my common controller code into a Plugin or Component? Or what would be the decision criteria?
  2. Is it possible to use multiple database tables from a controller or a component?
  3. Do Datasources or behaviours are suitable for this case.

Thank you in advance

2 Answers 2

1

It's hard to say what the best approach would be, given we don't know much about the database structure, what the controller methods do, and how they are used.

But one of your questions is very straightforward:

Is it possible to use multiple database tables from a controller or a component?

Yes, that is possible. Just create a model for each table (use public $useTable = 'tablename if cake cannot detect the table name automatically from your model names). Then, in AppController (considering your code will stay there), use this:

public $uses = array('List', 'all', 'models', 'you', 'need');
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. This solved my problem quickly. Now my AppController uses 3 different models for 3 tables.
1

Best way is (if possible) to redesign the database, because in cakephp a good database design resolve half of your problems. If this is not possible then second one is use Components and using this component you can you multiple database tables.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.