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 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?