trante
trante

Reputation: 34006

Using multiple database tables in CakePHP

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

Upvotes: 0

Views: 403

Answers (2)

bfavaretto
bfavaretto

Reputation: 71939

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');

Upvotes: 1

thecodeparadox
thecodeparadox

Reputation: 87073

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.

Upvotes: 1

Related Questions