如何在yii中生成模型中的条件关系变量

如何在yii中生成模型中的条件关系变量,yii,Yii,例如,我有3张桌子 1) Tab1有3个字段用于主要成员详细信息 ----tab1_id(PK) ----fname ----lname ----tab2_id(PK) ----fname ----lname ----tab1_id(FK) ----count 数据如下: 1-----test_firstname------test_lastname 2-----test2_firstname------test2_lastname 1----test1_firstname-----tes

例如,我有3张桌子

1) Tab1有3个字段用于主要成员详细信息

----tab1_id(PK)
----fname
----lname
----tab2_id(PK)
----fname
----lname
----tab1_id(FK)
----count
数据如下:

1-----test_firstname------test_lastname
2-----test2_firstname------test2_lastname
1----test1_firstname-----test1_lastname-----1-----1
2----test2_firstname-----test2_lastname-----1-----2
3----test1_firstname-----test1_lastname-----2-----1
2) Tab2有4个字段用于辅助成员详细信息

----tab1_id(PK)
----fname
----lname
----tab2_id(PK)
----fname
----lname
----tab1_id(FK)
----count
数据如下:

1-----test_firstname------test_lastname
2-----test2_firstname------test2_lastname
1----test1_firstname-----test1_lastname-----1-----1
2----test2_firstname-----test2_lastname-----1-----2
3----test1_firstname-----test1_lastname-----2-----1

3) Tab_数据,根据标志计数字段组合主要和次要的详细信息

----tab_data_id
----operation
----tab1_id(FK)
----count(FK)
现在,我如何为tab_数据模型类编写条件关系。 我想知道,如果count==0,则self使用tab1\u id字段属于tab1;如果count>0,则self使用tab1\u id和count字段属于tab2

这在YII中是可能的


谢谢。

是的。但这是一个糟糕的设计

'relation1' => array(self::BELONGS_TO, 'tab1', 'ref_id', 'condition' => 'count > 0'),
'relation2' => array(self::BELONGS_TO, 'tab2', 'ref_id', 'condition' => 'count = 0'),
请参阅本Yii论坛:

谢谢你的回复。。那么这个案例的处理方法是什么。。。!!必须管理单个表中的父子记录关系,并且必须基于引用建立关系。。??也可以为引用id传递多个字段。?对于父子关系,您可以按照我的答案原样进行。您不能为引用id传递多个字段(据我所知)。