duandi2853 2014-01-25 01:21
浏览 33
已采纳

ATK4:表中没有唯一的列

How do you implement a model based off of a database table that has no single column primary key? The table has a composite primary key but no unique column. I want to create a model with this table as its primary table, but as far as I can tell the model requires a unique id field.

The table holds custom field values for services and consists of the following 3 columns: field_id, service_id, value. I need to be able to reference all of the custom field values that exist for a service and also to reference all of the services linked to a particular value.

The two main problems I'm having are..

1) Constructing a model based on the custom fields values table which has no unique column

2) Accomplishing a join with two 'ON' conditions. For example:

services JOIN fields ON
    services.id = field.service_id
JOIN values ON
    field.id = values.field_id AND
    values.service_id = services.id
  • 写回答

1条回答 默认 最新

  • dqwh1202 2014-01-27 22:08
    关注

    1) See my and Romans answers in following question some days ago: Junction Table with mutiple primary keys

    2) That's simple - you should add next join not to model itself, but to previous "join"

    $j_fields = $services_model->join('fields');
    $j_values = $j_fields->join('values'); // $j_fields here not $services_model
    

    You an also define second condition (values.service_id=services.id) somehow, but to tell the truth I don't have working example ready to post. You have to use $model->dsql->andExpr() as far as I remember.

    I guess you can make it using addCondition (moving that condition to WHERE) or by using DSQL, too.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?