1

I am new in Cakephp. Please, how can I implement this logic in cakephp model knowing that cake doesn't accept composite key.

I have 3 Tables.

CREATE TABLE satelites(
    id INTEGER PRIMARY KEY,
    name TEXT,
    location TEXT);

CREATE TABLE sensors (
    satelite_id INTEGER, 
    type VARCHAR(100),
    unitofmeasurement TEXT,
    PRIMARY KEY(satelite_id,type),
    CONSTRAINT satelite_id FOREIGN KEY(satelite_id) 
        REFERENCES satelites(id) ON DELETE CASCADE
);

CREATE TABLE sensordatas(
    id INTEGER PRIMARY KEY AUTO_INCREMENT,
    satelite_id INTEGER, 
    sensortype VARCHAR(100), 
    value REAL,
    valuetext INTEGER, 
    timestamp TIMESTAMP NOT NULL,
    FOREIGN KEY(satelite_id,sensortype) 
       REFERENCES sensors(satelite_id,type)
);
  • satelites has many sensors
  • sensors belongs to satelites
  • sensors has many sensorsdatas
  • sensordatas belongs to sensors
Tiny
  • 27,221
  • 105
  • 339
  • 599
aagargoura
  • 396
  • 2
  • 6
  • 22
  • sensordatas belongs to sensors : with which field ? type ? – MouradK Nov 19 '14 at 10:20
  • sensordatas linked with sensors through 2 foreign key : satelite_id and sensortype. ==> FOREIGN KEY(satelite_id,sensortype) REFERENCES sensors(satelite_id,type) – aagargoura Nov 20 '14 at 11:25

2 Answers2

0

If i understand you, you had to rework your table schema for use all CakePHP power.

It s a good pratice to have an 1 primary key by table (for cakephp but for all tables in general)

So sensors must have id

and sensordatas : sensor_id

and after simple sensordata belongsTo sensorId.

MouradK
  • 1,527
  • 1
  • 11
  • 14
  • you will win a lot of time with this structure, and use a good pratices. see : http://stackoverflow.com/questions/337503/whats-the-best-practice-for-primary-keys-in-tables – MouradK Nov 20 '14 at 14:34
0

http://book.cakephp.org/3.0/en/quickstart.html#creating-the-database

CakePHP 3 now supports composite keys for whatever purposes as these dont help with record access. So as suggested, We must include the id primary key to all our tables for selection and updating.

Dawoodjee
  • 439
  • 5
  • 16