问题描述:

I'm developing a site like "yellow pages" with more complex DB than I had ever experienced before.

For example:

This DB has 3 tables:

1. organisations – with some general information.

2. services - with list of all services that can be provided.

3. relation table.

Organisations table structure:

id name city phone etc.

1 CompanyName Farewell 987-65-43

Services table structure:

id ServiceType

1 purchase

-----

2 sale

-----

3 exchange

etc..

you get the point

And relation table structure:

id OrganisationID ServId

1 1 2

----

2 1 3

----

3 1 6

----

4 2 3

In website output I need a list of all organisations with all the services they provide.

I can't fully understand (even after reading Linking Models Together section in the CookBook) how I should combine all this tables using Cake's syntax and conventions.

I'll be glad if anyone can provide me a Cake's model/model's (how many I need?) example with ability to retrieve this data.

网友答案:

I can't comment on your question so I'll ask here...

Organisations can have many services and services can have many organisations? Is it HABTM relationship? (Just checking...)

If it is, I'll then edit this and try to give you the answer. :)

EDIT:

TABLES

1. table: organisations
2. table: services
3. table: organisations_services (sorted alphabetically)


1. table: id, name, city...
2. table: id, service_type
3. table: id, organisation_id, service_id (singular, sorted alphabetically)

MODELS

Organisation - inside your organisation model you must have:

var $hasAndBelongsToMany = array('Service');

Service - inside your service model you must have:

var $hasAndBelongsToMany = array('Organisation');

And that's all. Everything else leave to the beautiful Cake. :) If you don't understand something, feel free to ask and here you can get more information.

相关阅读:
Top