加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

php – 使用主键以外的字段的Yii模型关系

发布时间:2020-12-11 23:43:37 所属栏目:MySql教程 来源:网络整理
导读:我需要根据不是主键的字段创建关系.许多如何执行此操作的示例基于一对多和多对多关系.我尝试过以下建议但没有成功 Relation in YII with not ID as primary key Yii CActiveRecord: find related data,but not using the primary key Yii Relations with non

我需要根据不是主键的字段创建关系.许多如何执行此操作的示例基于一对多和多对多关系.我尝试过以下建议但没有成功

Relation in YII with not “ID” as primary key

Yii CActiveRecord: find related data,but not using the primary key

Yii Relations with non-Primary keys

Yii Model Relation Join (HAS_ONE)

我有以下表结构:

+------+---------+-----------+
| id   |   name  | status_id |
+------+---------+-----------+
|  1   | service1| 1         |
+------+---------+-----------+
| 2    | service2| 2         |
+------+---------+-----------+

这是我的表active_service.我也有下表

+----------+----------+---------------------+-----------+
|id        |related_id|related_text         |  text     |
+----------+----------+---------------------+-----------+
|65        |1         |ActiveServices_status|  Open     |
+----------+----------+---------------------+-----------+
|72        |2         |ActiveServices_status|  Active   |
+----------+----------+---------------------+-----------+
|102       |3         |ActiveServices_status|  Closed   |
+----------+----------+---------------------+-----------+

这是我的related_fields表
此表包含用于下拉列表等的所有字段.related_text告诉我们它的用途,而related_id是状态的id,这是我需要链接的字段.因此,active_service表中的status_id与满足条件的related_fields表的related_id字段相关,即related_text设置为ActiveServices_status.我将如何创建这种关系.这是我迄今为止所做的最好的例子(在ActiveServices模型中).

public function relations()
{
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(

        'rl_status'=>array(self::BELONGS_TO,'RelatedFields','status_id','condition'=>'related_text = "ActiveServices_status"','on'=>'status_id = related_id'),);
}

任何帮助,将不胜感激. 最佳答案 因此,在尝试了大约100行不同的代码后,最终想出了这个问题.因此,这是对我有用的解决方案.

'rl_status' => array(self::BELONGS_TO,'','foreignKey' => array('status_id'=>'related_id'),

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读