简体   繁体   English

Cakephp复合主键

[英]Cakephp Composite Primary Key

I am new in Cakephp. 我是Cakephp的新手。 Please, how can I implement this logic in cakephp model knowing that cake doesn't accept composite key. 请知道知道蛋糕不接受复合键,如何在cakephp模型中实现此逻辑。

I have 3 Tables. 我有3张桌子。

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 sensordatas属于传感器

If i understand you, you had to rework your table schema for use all CakePHP power. 如果我了解您,则您必须重新整理表架构以使用所有CakePHP功能。

It sa good pratice to have an 1 primary key by table (for cakephp but for all tables in general) 按表有1个主键是一个好习惯 (对于cakephp,但对于所有一般表而言)

So sensors must have id 因此传感器必须具有ID

and sensordatas : sensor_id 和sensordatas:sensor_id

and after simple sensordata belongsTo sensorId. 然后在简单的sensordata所属到sensorId之后。

http://book.cakephp.org/3.0/en/quickstart.html#creating-the-database 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. CakePHP 3现在支持出于任何目的的组合键,因为这些键对记录访问没有帮助。 So as suggested, We must include the id primary key to all our tables for selection and updating. 因此,正如建议的那样,我们必须在所有表中都包含id主键,以进行选择和更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM