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

tereško
  • 58,060
  • 25
  • 98
  • 150
trante
  • 33,518
  • 47
  • 192
  • 272

2 Answers2

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.

thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
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');
bfavaretto
  • 71,580
  • 16
  • 111
  • 150
  • Thank you very much. This solved my problem quickly. Now my AppController uses 3 different models for 3 tables. – trante Apr 16 '12 at 20:06